@@ -24,64 +24,74 @@ static void fixI8TruncUseChain(Instruction &I,
24
24
SmallVectorImpl<Instruction *> &ToRemove,
25
25
DenseMap<Value *, Value *> &ReplacedValues) {
26
26
27
- auto *Cmp = dyn_cast<CmpInst>(&I);
28
-
29
- if (auto *Trunc = dyn_cast<TruncInst>(&I)) {
30
- if (Trunc->getDestTy ()->isIntegerTy (8 )) {
31
- ReplacedValues[Trunc] = Trunc->getOperand (0 );
32
- ToRemove.push_back (Trunc);
33
- }
34
- } else if (I.getType ()->isIntegerTy (8 ) ||
35
- (Cmp && Cmp->getOperand (0 )->getType ()->isIntegerTy (8 ))) {
36
- IRBuilder<> Builder (&I);
37
-
38
- SmallVector<Value *> NewOperands;
27
+ auto ProcessOperands = [&](SmallVector<Value *> &NewOperands) {
39
28
Type *InstrType = IntegerType::get (I.getContext (), 32 );
29
+
40
30
for (unsigned OpIdx = 0 ; OpIdx < I.getNumOperands (); ++OpIdx) {
41
31
Value *Op = I.getOperand (OpIdx);
42
32
if (ReplacedValues.count (Op))
43
33
InstrType = ReplacedValues[Op]->getType ();
44
34
}
35
+
45
36
for (unsigned OpIdx = 0 ; OpIdx < I.getNumOperands (); ++OpIdx) {
46
37
Value *Op = I.getOperand (OpIdx);
47
- if (ReplacedValues.count (Op))
38
+ if (ReplacedValues.count (Op)) {
48
39
NewOperands.push_back (ReplacedValues[Op]);
49
- else if (auto *Imm = dyn_cast<ConstantInt>(Op)) {
40
+ } else if (auto *Imm = dyn_cast<ConstantInt>(Op)) {
50
41
APInt Value = Imm->getValue ();
51
42
unsigned NewBitWidth = InstrType->getIntegerBitWidth ();
52
43
// Note: options here are sext or sextOrTrunc.
53
44
// Since i8 isn't supported, we assume new values
54
45
// will always have a higher bitness.
46
+ assert (NewBitWidth > Value.getBitWidth () &&
47
+ " Replacement's BitWidth should be larger than Current." );
55
48
APInt NewValue = Value.sext (NewBitWidth);
56
49
NewOperands.push_back (ConstantInt::get (InstrType, NewValue));
57
50
} else {
58
51
assert (!Op->getType ()->isIntegerTy (8 ));
59
52
NewOperands.push_back (Op);
60
53
}
61
54
}
62
-
63
- Value *NewInst = nullptr ;
64
- if (auto *BO = dyn_cast<BinaryOperator>(&I)) {
65
- NewInst =
66
- Builder.CreateBinOp (BO->getOpcode (), NewOperands[0 ], NewOperands[1 ]);
67
-
68
- if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(&I)) {
69
- if (OBO->hasNoSignedWrap ())
70
- cast<BinaryOperator>(NewInst)->setHasNoSignedWrap ();
71
- if (OBO->hasNoUnsignedWrap ())
72
- cast<BinaryOperator>(NewInst)->setHasNoUnsignedWrap ();
73
- }
74
- } else if (Cmp) {
75
- NewInst = Builder.CreateCmp (Cmp->getPredicate (), NewOperands[0 ],
76
- NewOperands[1 ]);
77
- Cmp->replaceAllUsesWith (NewInst);
55
+ };
56
+ IRBuilder<> Builder (&I);
57
+ if (auto *Trunc = dyn_cast<TruncInst>(&I)) {
58
+ if (Trunc->getDestTy ()->isIntegerTy (8 )) {
59
+ ReplacedValues[Trunc] = Trunc->getOperand (0 );
60
+ ToRemove.push_back (Trunc);
78
61
}
79
-
80
- if (NewInst) {
81
- ReplacedValues[&I] = NewInst;
82
- ToRemove.push_back (&I);
62
+ }
63
+ Value *NewInst = nullptr ;
64
+ if (auto *BO = dyn_cast<BinaryOperator>(&I)) {
65
+ if (!I.getType ()->isIntegerTy (8 ))
66
+ return ;
67
+ SmallVector<Value *> NewOperands;
68
+ ProcessOperands (NewOperands);
69
+ NewInst =
70
+ Builder.CreateBinOp (BO->getOpcode (), NewOperands[0 ], NewOperands[1 ]);
71
+ if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(&I)) {
72
+ if (OBO->hasNoSignedWrap ())
73
+ cast<BinaryOperator>(NewInst)->setHasNoSignedWrap ();
74
+ if (OBO->hasNoUnsignedWrap ())
75
+ cast<BinaryOperator>(NewInst)->setHasNoUnsignedWrap ();
83
76
}
84
- } else if (auto *Cast = dyn_cast<CastInst>(&I)) {
77
+ }
78
+
79
+ if (auto *Cmp = dyn_cast<CmpInst>(&I)) {
80
+ if (!Cmp->getOperand (0 )->getType ()->isIntegerTy (8 ))
81
+ return ;
82
+ SmallVector<Value *> NewOperands;
83
+ ProcessOperands (NewOperands);
84
+ NewInst =
85
+ Builder.CreateCmp (Cmp->getPredicate (), NewOperands[0 ], NewOperands[1 ]);
86
+ Cmp->replaceAllUsesWith (NewInst);
87
+ }
88
+ if (NewInst) {
89
+ ReplacedValues[&I] = NewInst;
90
+ ToRemove.push_back (&I);
91
+ return ;
92
+ }
93
+
94
+ if (auto *Cast = dyn_cast<CastInst>(&I)) {
85
95
if (Cast->getSrcTy ()->isIntegerTy (8 )) {
86
96
ToRemove.push_back (Cast);
87
97
Cast->replaceAllUsesWith (ReplacedValues[Cast->getOperand (0 )]);
0 commit comments