/
lower_128bit.rs
242 lines (220 loc) · 9.56 KB
/
lower_128bit.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Replaces 128-bit operators with lang item calls
use rustc::hir::def_id::DefId;
use rustc::middle::lang_items::LangItem;
use rustc::mir::*;
use rustc::ty::{Slice, Ty, TyCtxt, TypeVariants};
use rustc_data_structures::indexed_vec::{Idx};
use transform::{MirPass, MirSource};
use syntax;
pub struct Lower128Bit;
impl MirPass for Lower128Bit {
fn run_pass<'a, 'tcx>(&self,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
_src: MirSource,
mir: &mut Mir<'tcx>) {
let debugging_override = tcx.sess.opts.debugging_opts.lower_128bit_ops;
let target_default = tcx.sess.host.options.i128_lowering;
if !debugging_override.unwrap_or(target_default) {
return
}
self.lower_128bit_ops(tcx, mir);
}
}
impl Lower128Bit {
fn lower_128bit_ops<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, mir: &mut Mir<'tcx>) {
let mut new_blocks = Vec::new();
let cur_len = mir.basic_blocks().len();
let (basic_blocks, local_decls) = mir.basic_blocks_and_local_decls_mut();
for block in basic_blocks.iter_mut() {
for i in (0..block.statements.len()).rev() {
let (lang_item, rhs_kind) =
if let Some((lang_item, rhs_kind)) =
lower_to(&block.statements[i], local_decls, tcx)
{
(lang_item, rhs_kind)
} else {
continue;
};
let rhs_override_ty = rhs_kind.ty(tcx);
let cast_local =
match rhs_override_ty {
None => None,
Some(ty) => {
let local_decl = LocalDecl::new_internal(
ty, block.statements[i].source_info.span);
Some(local_decls.push(local_decl))
},
};
let storage_dead = cast_local.map(|local| {
Statement {
source_info: block.statements[i].source_info,
kind: StatementKind::StorageDead(local),
}
});
let after_call = BasicBlockData {
statements: storage_dead.into_iter()
.chain(block.statements.drain((i+1)..)).collect(),
is_cleanup: block.is_cleanup,
terminator: block.terminator.take(),
};
let bin_statement = block.statements.pop().unwrap();
let (source_info, place, lhs, mut rhs) = match bin_statement {
Statement {
source_info,
kind: StatementKind::Assign(
place,
Rvalue::BinaryOp(_, lhs, rhs))
} => (source_info, place, lhs, rhs),
Statement {
source_info,
kind: StatementKind::Assign(
place,
Rvalue::CheckedBinaryOp(_, lhs, rhs))
} => (source_info, place, lhs, rhs),
_ => bug!("Statement doesn't match pattern any more?"),
};
if let Some(local) = cast_local {
block.statements.push(Statement {
source_info: source_info,
kind: StatementKind::StorageLive(local),
});
block.statements.push(Statement {
source_info: source_info,
kind: StatementKind::Assign(
Place::Local(local),
Rvalue::Cast(
CastKind::Misc,
rhs,
rhs_override_ty.unwrap())),
});
rhs = Operand::Move(Place::Local(local));
}
let call_did = check_lang_item_type(
lang_item, &place, &lhs, &rhs, local_decls, tcx);
let bb = BasicBlock::new(cur_len + new_blocks.len());
new_blocks.push(after_call);
block.terminator =
Some(Terminator {
source_info,
kind: TerminatorKind::Call {
func: Operand::function_handle(tcx, call_did,
Slice::empty(), source_info.span),
args: vec![lhs, rhs],
destination: Some((place, bb)),
cleanup: None,
},
});
}
}
basic_blocks.extend(new_blocks);
}
}
fn check_lang_item_type<'a, 'tcx, D>(
lang_item: LangItem,
place: &Place<'tcx>,
lhs: &Operand<'tcx>,
rhs: &Operand<'tcx>,
local_decls: &D,
tcx: TyCtxt<'a, 'tcx, 'tcx>)
-> DefId
where D: HasLocalDecls<'tcx>
{
let did = tcx.require_lang_item(lang_item);
let poly_sig = tcx.fn_sig(did);
let sig = poly_sig.no_late_bound_regions().unwrap();
let lhs_ty = lhs.ty(local_decls, tcx);
let rhs_ty = rhs.ty(local_decls, tcx);
let place_ty = place.ty(local_decls, tcx).to_ty(tcx);
let expected = [lhs_ty, rhs_ty, place_ty];
assert_eq!(sig.inputs_and_output[..], expected,
"lang item {}", tcx.def_symbol_name(did));
did
}
fn lower_to<'a, 'tcx, D>(statement: &Statement<'tcx>, local_decls: &D, tcx: TyCtxt<'a, 'tcx, 'tcx>)
-> Option<(LangItem, RhsKind)>
where D: HasLocalDecls<'tcx>
{
match statement.kind {
StatementKind::Assign(_, Rvalue::BinaryOp(bin_op, ref lhs, _)) => {
let ty = lhs.ty(local_decls, tcx);
if let Some(is_signed) = sign_of_128bit(ty) {
return item_for_op(bin_op, is_signed);
}
},
StatementKind::Assign(_, Rvalue::CheckedBinaryOp(bin_op, ref lhs, _)) => {
let ty = lhs.ty(local_decls, tcx);
if let Some(is_signed) = sign_of_128bit(ty) {
return item_for_checked_op(bin_op, is_signed);
}
},
_ => {},
}
None
}
#[derive(Copy, Clone)]
enum RhsKind {
Unchanged,
ForceU128,
ForceU32,
}
impl RhsKind {
fn ty<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Option<Ty<'tcx>> {
match *self {
RhsKind::Unchanged => None,
RhsKind::ForceU128 => Some(tcx.types.u128),
RhsKind::ForceU32 => Some(tcx.types.u32),
}
}
}
fn sign_of_128bit(ty: Ty) -> Option<bool> {
match ty.sty {
TypeVariants::TyInt(syntax::ast::IntTy::I128) => Some(true),
TypeVariants::TyUint(syntax::ast::UintTy::U128) => Some(false),
_ => None,
}
}
fn item_for_op(bin_op: BinOp, is_signed: bool) -> Option<(LangItem, RhsKind)> {
let i = match (bin_op, is_signed) {
(BinOp::Add, true) => (LangItem::I128AddFnLangItem, RhsKind::Unchanged),
(BinOp::Add, false) => (LangItem::U128AddFnLangItem, RhsKind::Unchanged),
(BinOp::Sub, true) => (LangItem::I128SubFnLangItem, RhsKind::Unchanged),
(BinOp::Sub, false) => (LangItem::U128SubFnLangItem, RhsKind::Unchanged),
(BinOp::Mul, true) => (LangItem::I128MulFnLangItem, RhsKind::Unchanged),
(BinOp::Mul, false) => (LangItem::U128MulFnLangItem, RhsKind::Unchanged),
(BinOp::Div, true) => (LangItem::I128DivFnLangItem, RhsKind::Unchanged),
(BinOp::Div, false) => (LangItem::U128DivFnLangItem, RhsKind::Unchanged),
(BinOp::Rem, true) => (LangItem::I128RemFnLangItem, RhsKind::Unchanged),
(BinOp::Rem, false) => (LangItem::U128RemFnLangItem, RhsKind::Unchanged),
(BinOp::Shl, true) => (LangItem::I128ShlFnLangItem, RhsKind::ForceU32),
(BinOp::Shl, false) => (LangItem::U128ShlFnLangItem, RhsKind::ForceU32),
(BinOp::Shr, true) => (LangItem::I128ShrFnLangItem, RhsKind::ForceU32),
(BinOp::Shr, false) => (LangItem::U128ShrFnLangItem, RhsKind::ForceU32),
_ => return None,
};
Some(i)
}
fn item_for_checked_op(bin_op: BinOp, is_signed: bool) -> Option<(LangItem, RhsKind)> {
let i = match (bin_op, is_signed) {
(BinOp::Add, true) => (LangItem::I128AddoFnLangItem, RhsKind::Unchanged),
(BinOp::Add, false) => (LangItem::U128AddoFnLangItem, RhsKind::Unchanged),
(BinOp::Sub, true) => (LangItem::I128SuboFnLangItem, RhsKind::Unchanged),
(BinOp::Sub, false) => (LangItem::U128SuboFnLangItem, RhsKind::Unchanged),
(BinOp::Mul, true) => (LangItem::I128MuloFnLangItem, RhsKind::Unchanged),
(BinOp::Mul, false) => (LangItem::U128MuloFnLangItem, RhsKind::Unchanged),
(BinOp::Shl, true) => (LangItem::I128ShloFnLangItem, RhsKind::ForceU128),
(BinOp::Shl, false) => (LangItem::U128ShloFnLangItem, RhsKind::ForceU128),
(BinOp::Shr, true) => (LangItem::I128ShroFnLangItem, RhsKind::ForceU128),
(BinOp::Shr, false) => (LangItem::U128ShroFnLangItem, RhsKind::ForceU128),
_ => bug!("That should be all the checked ones?"),
};
Some(i)
}