1 //===----- TypePromotion.cpp ----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// This is an opcode based type promotion pass for small types that would
11 /// otherwise be promoted during legalisation. This works around the limitations
12 /// of selection dag for cyclic regions. The search begins from icmp
13 /// instructions operands where a tree, consisting of non-wrapping or safe
14 /// wrapping instructions, is built, checked and promoted if possible.
15 ///
16 //===----------------------------------------------------------------------===//
17 
18 #include "llvm/ADT/SetVector.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Analysis/TargetTransformInfo.h"
21 #include "llvm/CodeGen/Passes.h"
22 #include "llvm/CodeGen/TargetLowering.h"
23 #include "llvm/CodeGen/TargetPassConfig.h"
24 #include "llvm/CodeGen/TargetSubtargetInfo.h"
25 #include "llvm/IR/Attributes.h"
26 #include "llvm/IR/BasicBlock.h"
27 #include "llvm/IR/IRBuilder.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/DataLayout.h"
30 #include "llvm/IR/InstrTypes.h"
31 #include "llvm/IR/Instruction.h"
32 #include "llvm/IR/Instructions.h"
33 #include "llvm/IR/Type.h"
34 #include "llvm/IR/Value.h"
35 #include "llvm/IR/Verifier.h"
36 #include "llvm/InitializePasses.h"
37 #include "llvm/Pass.h"
38 #include "llvm/Support/Casting.h"
39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Target/TargetMachine.h"
41 
42 #define DEBUG_TYPE "type-promotion"
43 #define PASS_NAME "Type Promotion"
44 
45 using namespace llvm;
46 
47 static cl::opt<bool>
48 DisablePromotion("disable-type-promotion", cl::Hidden, cl::init(false),
49                  cl::desc("Disable type promotion pass"));
50 
51 // The goal of this pass is to enable more efficient code generation for
52 // operations on narrow types (i.e. types with < 32-bits) and this is a
53 // motivating IR code example:
54 //
55 //   define hidden i32 @cmp(i8 zeroext) {
56 //     %2 = add i8 %0, -49
57 //     %3 = icmp ult i8 %2, 3
58 //     ..
59 //   }
60 //
61 // The issue here is that i8 is type-legalized to i32 because i8 is not a
62 // legal type. Thus, arithmetic is done in integer-precision, but then the
63 // byte value is masked out as follows:
64 //
65 //   t19: i32 = add t4, Constant:i32<-49>
66 //     t24: i32 = and t19, Constant:i32<255>
67 //
68 // Consequently, we generate code like this:
69 //
70 //   subs  r0, #49
71 //   uxtb  r1, r0
72 //   cmp r1, #3
73 //
74 // This shows that masking out the byte value results in generation of
75 // the UXTB instruction. This is not optimal as r0 already contains the byte
76 // value we need, and so instead we can just generate:
77 //
78 //   sub.w r1, r0, #49
79 //   cmp r1, #3
80 //
81 // We achieve this by type promoting the IR to i32 like so for this example:
82 //
83 //   define i32 @cmp(i8 zeroext %c) {
84 //     %0 = zext i8 %c to i32
85 //     %c.off = add i32 %0, -49
86 //     %1 = icmp ult i32 %c.off, 3
87 //     ..
88 //   }
89 //
90 // For this to be valid and legal, we need to prove that the i32 add is
91 // producing the same value as the i8 addition, and that e.g. no overflow
92 // happens.
93 //
94 // A brief sketch of the algorithm and some terminology.
95 // We pattern match interesting IR patterns:
96 // - which have "sources": instructions producing narrow values (i8, i16), and
97 // - they have "sinks": instructions consuming these narrow values.
98 //
99 // We collect all instruction connecting sources and sinks in a worklist, so
100 // that we can mutate these instruction and perform type promotion when it is
101 // legal to do so.
102 
103 namespace {
104 class IRPromoter {
105   LLVMContext &Ctx;
106   IntegerType *OrigTy = nullptr;
107   unsigned PromotedWidth = 0;
108   SetVector<Value*> &Visited;
109   SetVector<Value*> &Sources;
110   SetVector<Instruction*> &Sinks;
111   SmallVectorImpl<Instruction*> &SafeWrap;
112   IntegerType *ExtTy = nullptr;
113   SmallPtrSet<Value*, 8> NewInsts;
114   SmallPtrSet<Instruction*, 4> InstsToRemove;
115   DenseMap<Value*, SmallVector<Type*, 4>> TruncTysMap;
116   SmallPtrSet<Value*, 8> Promoted;
117 
118   void ReplaceAllUsersOfWith(Value *From, Value *To);
119   void PrepareWrappingAdds(void);
120   void ExtendSources(void);
121   void ConvertTruncs(void);
122   void PromoteTree(void);
123   void TruncateSinks(void);
124   void Cleanup(void);
125 
126 public:
127   IRPromoter(LLVMContext &C, IntegerType *Ty, unsigned Width,
128              SetVector<Value*> &visited, SetVector<Value*> &sources,
129              SetVector<Instruction*> &sinks,
130              SmallVectorImpl<Instruction*> &wrap) :
131     Ctx(C), OrigTy(Ty), PromotedWidth(Width), Visited(visited),
132     Sources(sources), Sinks(sinks), SafeWrap(wrap) {
133     ExtTy = IntegerType::get(Ctx, PromotedWidth);
134     assert(OrigTy->getPrimitiveSizeInBits().getFixedSize() <
135                ExtTy->getPrimitiveSizeInBits().getFixedSize() &&
136            "Original type not smaller than extended type");
137   }
138 
139   void Mutate();
140 };
141 
142 class TypePromotion : public FunctionPass {
143   unsigned TypeSize = 0;
144   LLVMContext *Ctx = nullptr;
145   unsigned RegisterBitWidth = 0;
146   SmallPtrSet<Value*, 16> AllVisited;
147   SmallPtrSet<Instruction*, 8> SafeToPromote;
148   SmallVector<Instruction*, 4> SafeWrap;
149 
150   // Does V have the same size result type as TypeSize.
151   bool EqualTypeSize(Value *V);
152   // Does V have the same size, or narrower, result type as TypeSize.
153   bool LessOrEqualTypeSize(Value *V);
154   // Does V have a result type that is wider than TypeSize.
155   bool GreaterThanTypeSize(Value *V);
156   // Does V have a result type that is narrower than TypeSize.
157   bool LessThanTypeSize(Value *V);
158   // Should V be a leaf in the promote tree?
159   bool isSource(Value *V);
160   // Should V be a root in the promotion tree?
161   bool isSink(Value *V);
162   // Should we change the result type of V? It will result in the users of V
163   // being visited.
164   bool shouldPromote(Value *V);
165   // Is I an add or a sub, which isn't marked as nuw, but where a wrapping
166   // result won't affect the computation?
167   bool isSafeWrap(Instruction *I);
168   // Can V have its integer type promoted, or can the type be ignored.
169   bool isSupportedType(Value *V);
170   // Is V an instruction with a supported opcode or another value that we can
171   // handle, such as constants and basic blocks.
172   bool isSupportedValue(Value *V);
173   // Is V an instruction thats result can trivially promoted, or has safe
174   // wrapping.
175   bool isLegalToPromote(Value *V);
176   bool TryToPromote(Value *V, unsigned PromotedWidth);
177 
178 public:
179   static char ID;
180 
181   TypePromotion() : FunctionPass(ID) {}
182 
183   void getAnalysisUsage(AnalysisUsage &AU) const override {
184     AU.addRequired<TargetTransformInfoWrapperPass>();
185     AU.addRequired<TargetPassConfig>();
186   }
187 
188   StringRef getPassName() const override { return PASS_NAME; }
189 
190   bool runOnFunction(Function &F) override;
191 };
192 
193 }
194 
195 static bool GenerateSignBits(Instruction *I) {
196   unsigned Opc = I->getOpcode();
197   return Opc == Instruction::AShr || Opc == Instruction::SDiv ||
198          Opc == Instruction::SRem || Opc == Instruction::SExt;
199 }
200 
201 bool TypePromotion::EqualTypeSize(Value *V) {
202   return V->getType()->getScalarSizeInBits() == TypeSize;
203 }
204 
205 bool TypePromotion::LessOrEqualTypeSize(Value *V) {
206   return V->getType()->getScalarSizeInBits() <= TypeSize;
207 }
208 
209 bool TypePromotion::GreaterThanTypeSize(Value *V) {
210   return V->getType()->getScalarSizeInBits() > TypeSize;
211 }
212 
213 bool TypePromotion::LessThanTypeSize(Value *V) {
214   return V->getType()->getScalarSizeInBits() < TypeSize;
215 }
216 
217 /// Return true if the given value is a source in the use-def chain, producing
218 /// a narrow 'TypeSize' value. These values will be zext to start the promotion
219 /// of the tree to i32. We guarantee that these won't populate the upper bits
220 /// of the register. ZExt on the loads will be free, and the same for call
221 /// return values because we only accept ones that guarantee a zeroext ret val.
222 /// Many arguments will have the zeroext attribute too, so those would be free
223 /// too.
224 bool TypePromotion::isSource(Value *V) {
225   if (!isa<IntegerType>(V->getType()))
226     return false;
227 
228   // TODO Allow zext to be sources.
229   if (isa<Argument>(V))
230     return true;
231   else if (isa<LoadInst>(V))
232     return true;
233   else if (isa<BitCastInst>(V))
234     return true;
235   else if (auto *Call = dyn_cast<CallInst>(V))
236     return Call->hasRetAttr(Attribute::AttrKind::ZExt);
237   else if (auto *Trunc = dyn_cast<TruncInst>(V))
238     return EqualTypeSize(Trunc);
239   return false;
240 }
241 
242 /// Return true if V will require any promoted values to be truncated for the
243 /// the IR to remain valid. We can't mutate the value type of these
244 /// instructions.
245 bool TypePromotion::isSink(Value *V) {
246   // TODO The truncate also isn't actually necessary because we would already
247   // proved that the data value is kept within the range of the original data
248   // type.
249 
250   // Sinks are:
251   // - points where the value in the register is being observed, such as an
252   //   icmp, switch or store.
253   // - points where value types have to match, such as calls and returns.
254   // - zext are included to ease the transformation and are generally removed
255   //   later on.
256   if (auto *Store = dyn_cast<StoreInst>(V))
257     return LessOrEqualTypeSize(Store->getValueOperand());
258   if (auto *Return = dyn_cast<ReturnInst>(V))
259     return LessOrEqualTypeSize(Return->getReturnValue());
260   if (auto *ZExt = dyn_cast<ZExtInst>(V))
261     return GreaterThanTypeSize(ZExt);
262   if (auto *Switch = dyn_cast<SwitchInst>(V))
263     return LessThanTypeSize(Switch->getCondition());
264   if (auto *ICmp = dyn_cast<ICmpInst>(V))
265     return ICmp->isSigned() || LessThanTypeSize(ICmp->getOperand(0));
266 
267   return isa<CallInst>(V);
268 }
269 
270 /// Return whether this instruction can safely wrap.
271 bool TypePromotion::isSafeWrap(Instruction *I) {
272   // We can support a, potentially, wrapping instruction (I) if:
273   // - It is only used by an unsigned icmp.
274   // - The icmp uses a constant.
275   // - The wrapping value (I) is decreasing, i.e would underflow - wrapping
276   //   around zero to become a larger number than before.
277   // - The wrapping instruction (I) also uses a constant.
278   //
279   // We can then use the two constants to calculate whether the result would
280   // wrap in respect to itself in the original bitwidth. If it doesn't wrap,
281   // just underflows the range, the icmp would give the same result whether the
282   // result has been truncated or not. We calculate this by:
283   // - Zero extending both constants, if needed, to 32-bits.
284   // - Take the absolute value of I's constant, adding this to the icmp const.
285   // - Check that this value is not out of range for small type. If it is, it
286   //   means that it has underflowed enough to wrap around the icmp constant.
287   //
288   // For example:
289   //
290   // %sub = sub i8 %a, 2
291   // %cmp = icmp ule i8 %sub, 254
292   //
293   // If %a = 0, %sub = -2 == FE == 254
294   // But if this is evalulated as a i32
295   // %sub = -2 == FF FF FF FE == 4294967294
296   // So the unsigned compares (i8 and i32) would not yield the same result.
297   //
298   // Another way to look at it is:
299   // %a - 2 <= 254
300   // %a + 2 <= 254 + 2
301   // %a <= 256
302   // And we can't represent 256 in the i8 format, so we don't support it.
303   //
304   // Whereas:
305   //
306   // %sub i8 %a, 1
307   // %cmp = icmp ule i8 %sub, 254
308   //
309   // If %a = 0, %sub = -1 == FF == 255
310   // As i32:
311   // %sub = -1 == FF FF FF FF == 4294967295
312   //
313   // In this case, the unsigned compare results would be the same and this
314   // would also be true for ult, uge and ugt:
315   // - (255 < 254) == (0xFFFFFFFF < 254) == false
316   // - (255 <= 254) == (0xFFFFFFFF <= 254) == false
317   // - (255 > 254) == (0xFFFFFFFF > 254) == true
318   // - (255 >= 254) == (0xFFFFFFFF >= 254) == true
319   //
320   // To demonstrate why we can't handle increasing values:
321   //
322   // %add = add i8 %a, 2
323   // %cmp = icmp ult i8 %add, 127
324   //
325   // If %a = 254, %add = 256 == (i8 1)
326   // As i32:
327   // %add = 256
328   //
329   // (1 < 127) != (256 < 127)
330 
331   unsigned Opc = I->getOpcode();
332   if (Opc != Instruction::Add && Opc != Instruction::Sub)
333     return false;
334 
335   if (!I->hasOneUse() ||
336       !isa<ICmpInst>(*I->user_begin()) ||
337       !isa<ConstantInt>(I->getOperand(1)))
338     return false;
339 
340   ConstantInt *OverflowConst = cast<ConstantInt>(I->getOperand(1));
341   bool NegImm = OverflowConst->isNegative();
342   bool IsDecreasing = ((Opc == Instruction::Sub) && !NegImm) ||
343                        ((Opc == Instruction::Add) && NegImm);
344   if (!IsDecreasing)
345     return false;
346 
347   // Don't support an icmp that deals with sign bits.
348   auto *CI = cast<ICmpInst>(*I->user_begin());
349   if (CI->isSigned() || CI->isEquality())
350     return false;
351 
352   ConstantInt *ICmpConst = nullptr;
353   if (auto *Const = dyn_cast<ConstantInt>(CI->getOperand(0)))
354     ICmpConst = Const;
355   else if (auto *Const = dyn_cast<ConstantInt>(CI->getOperand(1)))
356     ICmpConst = Const;
357   else
358     return false;
359 
360   // Now check that the result can't wrap on itself.
361   APInt Total = ICmpConst->getValue().getBitWidth() < 32 ?
362     ICmpConst->getValue().zext(32) : ICmpConst->getValue();
363 
364   Total += OverflowConst->getValue().getBitWidth() < 32 ?
365     OverflowConst->getValue().abs().zext(32) : OverflowConst->getValue().abs();
366 
367   APInt Max = APInt::getAllOnesValue(TypePromotion::TypeSize);
368 
369   if (Total.getBitWidth() > Max.getBitWidth()) {
370     if (Total.ugt(Max.zext(Total.getBitWidth())))
371       return false;
372   } else if (Max.getBitWidth() > Total.getBitWidth()) {
373     if (Total.zext(Max.getBitWidth()).ugt(Max))
374       return false;
375   } else if (Total.ugt(Max))
376     return false;
377 
378   LLVM_DEBUG(dbgs() << "IR Promotion: Allowing safe overflow for "
379              << *I << "\n");
380   SafeWrap.push_back(I);
381   return true;
382 }
383 
384 bool TypePromotion::shouldPromote(Value *V) {
385   if (!isa<IntegerType>(V->getType()) || isSink(V))
386     return false;
387 
388   if (isSource(V))
389     return true;
390 
391   auto *I = dyn_cast<Instruction>(V);
392   if (!I)
393     return false;
394 
395   if (isa<ICmpInst>(I))
396     return false;
397 
398   return true;
399 }
400 
401 /// Return whether we can safely mutate V's type to ExtTy without having to be
402 /// concerned with zero extending or truncation.
403 static bool isPromotedResultSafe(Instruction *I) {
404   if (GenerateSignBits(I))
405     return false;
406 
407   if (!isa<OverflowingBinaryOperator>(I))
408     return true;
409 
410   return I->hasNoUnsignedWrap();
411 }
412 
413 void IRPromoter::ReplaceAllUsersOfWith(Value *From, Value *To) {
414   SmallVector<Instruction*, 4> Users;
415   Instruction *InstTo = dyn_cast<Instruction>(To);
416   bool ReplacedAll = true;
417 
418   LLVM_DEBUG(dbgs() << "IR Promotion: Replacing " << *From << " with " << *To
419              << "\n");
420 
421   for (Use &U : From->uses()) {
422     auto *User = cast<Instruction>(U.getUser());
423     if (InstTo && User->isIdenticalTo(InstTo)) {
424       ReplacedAll = false;
425       continue;
426     }
427     Users.push_back(User);
428   }
429 
430   for (auto *U : Users)
431     U->replaceUsesOfWith(From, To);
432 
433   if (ReplacedAll)
434     if (auto *I = dyn_cast<Instruction>(From))
435       InstsToRemove.insert(I);
436 }
437 
438 void IRPromoter::PrepareWrappingAdds() {
439   LLVM_DEBUG(dbgs() << "IR Promotion: Prepare wrapping adds.\n");
440   IRBuilder<> Builder{Ctx};
441 
442   // For adds that safely wrap and use a negative immediate as operand 1, we
443   // create an equivalent instruction using a positive immediate.
444   // That positive immediate can then be zext along with all the other
445   // immediates later.
446   for (auto *I : SafeWrap) {
447     if (I->getOpcode() != Instruction::Add)
448       continue;
449 
450     LLVM_DEBUG(dbgs() << "IR Promotion: Adjusting " << *I << "\n");
451     assert((isa<ConstantInt>(I->getOperand(1)) &&
452             cast<ConstantInt>(I->getOperand(1))->isNegative()) &&
453            "Wrapping should have a negative immediate as the second operand");
454 
455     auto Const = cast<ConstantInt>(I->getOperand(1));
456     auto *NewConst = ConstantInt::get(Ctx, Const->getValue().abs());
457     Builder.SetInsertPoint(I);
458     Value *NewVal = Builder.CreateSub(I->getOperand(0), NewConst);
459     if (auto *NewInst = dyn_cast<Instruction>(NewVal)) {
460       NewInst->copyIRFlags(I);
461       NewInsts.insert(NewInst);
462     }
463     InstsToRemove.insert(I);
464     I->replaceAllUsesWith(NewVal);
465     LLVM_DEBUG(dbgs() << "IR Promotion: New equivalent: " << *NewVal << "\n");
466   }
467   for (auto *I : NewInsts)
468     Visited.insert(I);
469 }
470 
471 void IRPromoter::ExtendSources() {
472   IRBuilder<> Builder{Ctx};
473 
474   auto InsertZExt = [&](Value *V, Instruction *InsertPt) {
475     assert(V->getType() != ExtTy && "zext already extends to i32");
476     LLVM_DEBUG(dbgs() << "IR Promotion: Inserting ZExt for " << *V << "\n");
477     Builder.SetInsertPoint(InsertPt);
478     if (auto *I = dyn_cast<Instruction>(V))
479       Builder.SetCurrentDebugLocation(I->getDebugLoc());
480 
481     Value *ZExt = Builder.CreateZExt(V, ExtTy);
482     if (auto *I = dyn_cast<Instruction>(ZExt)) {
483       if (isa<Argument>(V))
484         I->moveBefore(InsertPt);
485       else
486         I->moveAfter(InsertPt);
487       NewInsts.insert(I);
488     }
489 
490     ReplaceAllUsersOfWith(V, ZExt);
491   };
492 
493   // Now, insert extending instructions between the sources and their users.
494   LLVM_DEBUG(dbgs() << "IR Promotion: Promoting sources:\n");
495   for (auto V : Sources) {
496     LLVM_DEBUG(dbgs() << " - " << *V << "\n");
497     if (auto *I = dyn_cast<Instruction>(V))
498       InsertZExt(I, I);
499     else if (auto *Arg = dyn_cast<Argument>(V)) {
500       BasicBlock &BB = Arg->getParent()->front();
501       InsertZExt(Arg, &*BB.getFirstInsertionPt());
502     } else {
503       llvm_unreachable("unhandled source that needs extending");
504     }
505     Promoted.insert(V);
506   }
507 }
508 
509 void IRPromoter::PromoteTree() {
510   LLVM_DEBUG(dbgs() << "IR Promotion: Mutating the tree..\n");
511 
512   IRBuilder<> Builder{Ctx};
513 
514   // Mutate the types of the instructions within the tree. Here we handle
515   // constant operands.
516   for (auto *V : Visited) {
517     if (Sources.count(V))
518       continue;
519 
520     auto *I = cast<Instruction>(V);
521     if (Sinks.count(I))
522       continue;
523 
524     for (unsigned i = 0, e = I->getNumOperands(); i < e; ++i) {
525       Value *Op = I->getOperand(i);
526       if ((Op->getType() == ExtTy) || !isa<IntegerType>(Op->getType()))
527         continue;
528 
529       if (auto *Const = dyn_cast<ConstantInt>(Op)) {
530         Constant *NewConst = ConstantExpr::getZExt(Const, ExtTy);
531         I->setOperand(i, NewConst);
532       } else if (isa<UndefValue>(Op))
533         I->setOperand(i, UndefValue::get(ExtTy));
534     }
535 
536     // Mutate the result type, unless this is an icmp or switch.
537     if (!isa<ICmpInst>(I) && !isa<SwitchInst>(I)) {
538       I->mutateType(ExtTy);
539       Promoted.insert(I);
540     }
541   }
542 }
543 
544 void IRPromoter::TruncateSinks() {
545   LLVM_DEBUG(dbgs() << "IR Promotion: Fixing up the sinks:\n");
546 
547   IRBuilder<> Builder{Ctx};
548 
549   auto InsertTrunc = [&](Value *V, Type *TruncTy) -> Instruction* {
550     if (!isa<Instruction>(V) || !isa<IntegerType>(V->getType()))
551       return nullptr;
552 
553     if ((!Promoted.count(V) && !NewInsts.count(V)) || Sources.count(V))
554       return nullptr;
555 
556     LLVM_DEBUG(dbgs() << "IR Promotion: Creating " << *TruncTy << " Trunc for "
557                << *V << "\n");
558     Builder.SetInsertPoint(cast<Instruction>(V));
559     auto *Trunc = dyn_cast<Instruction>(Builder.CreateTrunc(V, TruncTy));
560     if (Trunc)
561       NewInsts.insert(Trunc);
562     return Trunc;
563   };
564 
565   // Fix up any stores or returns that use the results of the promoted
566   // chain.
567   for (auto I : Sinks) {
568     LLVM_DEBUG(dbgs() << "IR Promotion: For Sink: " << *I << "\n");
569 
570     // Handle calls separately as we need to iterate over arg operands.
571     if (auto *Call = dyn_cast<CallInst>(I)) {
572       for (unsigned i = 0; i < Call->getNumArgOperands(); ++i) {
573         Value *Arg = Call->getArgOperand(i);
574         Type *Ty = TruncTysMap[Call][i];
575         if (Instruction *Trunc = InsertTrunc(Arg, Ty)) {
576           Trunc->moveBefore(Call);
577           Call->setArgOperand(i, Trunc);
578         }
579       }
580       continue;
581     }
582 
583     // Special case switches because we need to truncate the condition.
584     if (auto *Switch = dyn_cast<SwitchInst>(I)) {
585       Type *Ty = TruncTysMap[Switch][0];
586       if (Instruction *Trunc = InsertTrunc(Switch->getCondition(), Ty)) {
587         Trunc->moveBefore(Switch);
588         Switch->setCondition(Trunc);
589       }
590       continue;
591     }
592 
593     // Now handle the others.
594     for (unsigned i = 0; i < I->getNumOperands(); ++i) {
595       Type *Ty = TruncTysMap[I][i];
596       if (Instruction *Trunc = InsertTrunc(I->getOperand(i), Ty)) {
597         Trunc->moveBefore(I);
598         I->setOperand(i, Trunc);
599       }
600     }
601   }
602 }
603 
604 void IRPromoter::Cleanup() {
605   LLVM_DEBUG(dbgs() << "IR Promotion: Cleanup..\n");
606   // Some zexts will now have become redundant, along with their trunc
607   // operands, so remove them
608   for (auto V : Visited) {
609     if (!isa<ZExtInst>(V))
610       continue;
611 
612     auto ZExt = cast<ZExtInst>(V);
613     if (ZExt->getDestTy() != ExtTy)
614       continue;
615 
616     Value *Src = ZExt->getOperand(0);
617     if (ZExt->getSrcTy() == ZExt->getDestTy()) {
618       LLVM_DEBUG(dbgs() << "IR Promotion: Removing unnecessary cast: " << *ZExt
619                  << "\n");
620       ReplaceAllUsersOfWith(ZExt, Src);
621       continue;
622     }
623 
624     // Unless they produce a value that is narrower than ExtTy, we can
625     // replace the result of the zext with the input of a newly inserted
626     // trunc.
627     if (NewInsts.count(Src) && isa<TruncInst>(Src) &&
628         Src->getType() == OrigTy) {
629       auto *Trunc = cast<TruncInst>(Src);
630       assert(Trunc->getOperand(0)->getType() == ExtTy &&
631              "expected inserted trunc to be operating on i32");
632       ReplaceAllUsersOfWith(ZExt, Trunc->getOperand(0));
633     }
634   }
635 
636   for (auto *I : InstsToRemove) {
637     LLVM_DEBUG(dbgs() << "IR Promotion: Removing " << *I << "\n");
638     I->dropAllReferences();
639     I->eraseFromParent();
640   }
641 }
642 
643 void IRPromoter::ConvertTruncs() {
644   LLVM_DEBUG(dbgs() << "IR Promotion: Converting truncs..\n");
645   IRBuilder<> Builder{Ctx};
646 
647   for (auto *V : Visited) {
648     if (!isa<TruncInst>(V) || Sources.count(V))
649       continue;
650 
651     auto *Trunc = cast<TruncInst>(V);
652     Builder.SetInsertPoint(Trunc);
653     IntegerType *SrcTy = cast<IntegerType>(Trunc->getOperand(0)->getType());
654     IntegerType *DestTy = cast<IntegerType>(TruncTysMap[Trunc][0]);
655 
656     unsigned NumBits = DestTy->getScalarSizeInBits();
657     ConstantInt *Mask =
658       ConstantInt::get(SrcTy, APInt::getMaxValue(NumBits).getZExtValue());
659     Value *Masked = Builder.CreateAnd(Trunc->getOperand(0), Mask);
660 
661     if (auto *I = dyn_cast<Instruction>(Masked))
662       NewInsts.insert(I);
663 
664     ReplaceAllUsersOfWith(Trunc, Masked);
665   }
666 }
667 
668 void IRPromoter::Mutate() {
669   LLVM_DEBUG(dbgs() << "IR Promotion: Promoting use-def chains from "
670              << OrigTy->getBitWidth() << " to " << PromotedWidth << "-bits\n");
671 
672   // Cache original types of the values that will likely need truncating
673   for (auto *I : Sinks) {
674     if (auto *Call = dyn_cast<CallInst>(I)) {
675       for (unsigned i = 0; i < Call->getNumArgOperands(); ++i) {
676         Value *Arg = Call->getArgOperand(i);
677         TruncTysMap[Call].push_back(Arg->getType());
678       }
679     } else if (auto *Switch = dyn_cast<SwitchInst>(I))
680       TruncTysMap[I].push_back(Switch->getCondition()->getType());
681     else {
682       for (unsigned i = 0; i < I->getNumOperands(); ++i)
683         TruncTysMap[I].push_back(I->getOperand(i)->getType());
684     }
685   }
686   for (auto *V : Visited) {
687     if (!isa<TruncInst>(V) || Sources.count(V))
688       continue;
689     auto *Trunc = cast<TruncInst>(V);
690     TruncTysMap[Trunc].push_back(Trunc->getDestTy());
691   }
692 
693   // Convert adds using negative immediates to equivalent instructions that use
694   // positive constants.
695   PrepareWrappingAdds();
696 
697   // Insert zext instructions between sources and their users.
698   ExtendSources();
699 
700   // Promote visited instructions, mutating their types in place.
701   PromoteTree();
702 
703   // Convert any truncs, that aren't sources, into AND masks.
704   ConvertTruncs();
705 
706   // Insert trunc instructions for use by calls, stores etc...
707   TruncateSinks();
708 
709   // Finally, remove unecessary zexts and truncs, delete old instructions and
710   // clear the data structures.
711   Cleanup();
712 
713   LLVM_DEBUG(dbgs() << "IR Promotion: Mutation complete\n");
714 }
715 
716 /// We disallow booleans to make life easier when dealing with icmps but allow
717 /// any other integer that fits in a scalar register. Void types are accepted
718 /// so we can handle switches.
719 bool TypePromotion::isSupportedType(Value *V) {
720   Type *Ty = V->getType();
721 
722   // Allow voids and pointers, these won't be promoted.
723   if (Ty->isVoidTy() || Ty->isPointerTy())
724     return true;
725 
726   if (!isa<IntegerType>(Ty) ||
727       cast<IntegerType>(Ty)->getBitWidth() == 1 ||
728       cast<IntegerType>(Ty)->getBitWidth() > RegisterBitWidth)
729     return false;
730 
731   return LessOrEqualTypeSize(V);
732 }
733 
734 /// We accept most instructions, as well as Arguments and ConstantInsts. We
735 /// Disallow casts other than zext and truncs and only allow calls if their
736 /// return value is zeroext. We don't allow opcodes that can introduce sign
737 /// bits.
738 bool TypePromotion::isSupportedValue(Value *V) {
739   if (auto *I = dyn_cast<Instruction>(V)) {
740     switch (I->getOpcode()) {
741     default:
742       return isa<BinaryOperator>(I) && isSupportedType(I) &&
743              !GenerateSignBits(I);
744     case Instruction::GetElementPtr:
745     case Instruction::Store:
746     case Instruction::Br:
747     case Instruction::Switch:
748       return true;
749     case Instruction::PHI:
750     case Instruction::Select:
751     case Instruction::Ret:
752     case Instruction::Load:
753     case Instruction::Trunc:
754     case Instruction::BitCast:
755       return isSupportedType(I);
756     case Instruction::ZExt:
757       return isSupportedType(I->getOperand(0));
758     case Instruction::ICmp:
759       // Now that we allow small types than TypeSize, only allow icmp of
760       // TypeSize because they will require a trunc to be legalised.
761       // TODO: Allow icmp of smaller types, and calculate at the end
762       // whether the transform would be beneficial.
763       if (isa<PointerType>(I->getOperand(0)->getType()))
764         return true;
765       return EqualTypeSize(I->getOperand(0));
766     case Instruction::Call: {
767       // Special cases for calls as we need to check for zeroext
768       // TODO We should accept calls even if they don't have zeroext, as they
769       // can still be sinks.
770       auto *Call = cast<CallInst>(I);
771       return isSupportedType(Call) &&
772              Call->hasRetAttr(Attribute::AttrKind::ZExt);
773     }
774     }
775   } else if (isa<Constant>(V) && !isa<ConstantExpr>(V)) {
776     return isSupportedType(V);
777   } else if (isa<Argument>(V))
778     return isSupportedType(V);
779 
780   return isa<BasicBlock>(V);
781 }
782 
783 /// Check that the type of V would be promoted and that the original type is
784 /// smaller than the targeted promoted type. Check that we're not trying to
785 /// promote something larger than our base 'TypeSize' type.
786 bool TypePromotion::isLegalToPromote(Value *V) {
787 
788   auto *I = dyn_cast<Instruction>(V);
789   if (!I)
790     return true;
791 
792   if (SafeToPromote.count(I))
793    return true;
794 
795   if (isPromotedResultSafe(I) || isSafeWrap(I)) {
796     SafeToPromote.insert(I);
797     return true;
798   }
799   return false;
800 }
801 
802 bool TypePromotion::TryToPromote(Value *V, unsigned PromotedWidth) {
803   Type *OrigTy = V->getType();
804   TypeSize = OrigTy->getPrimitiveSizeInBits().getFixedSize();
805   SafeToPromote.clear();
806   SafeWrap.clear();
807 
808   if (!isSupportedValue(V) || !shouldPromote(V) || !isLegalToPromote(V))
809     return false;
810 
811   LLVM_DEBUG(dbgs() << "IR Promotion: TryToPromote: " << *V << ", from "
812              << TypeSize << " bits to " << PromotedWidth << "\n");
813 
814   SetVector<Value*> WorkList;
815   SetVector<Value*> Sources;
816   SetVector<Instruction*> Sinks;
817   SetVector<Value*> CurrentVisited;
818   WorkList.insert(V);
819 
820   // Return true if V was added to the worklist as a supported instruction,
821   // if it was already visited, or if we don't need to explore it (e.g.
822   // pointer values and GEPs), and false otherwise.
823   auto AddLegalInst = [&](Value *V) {
824     if (CurrentVisited.count(V))
825       return true;
826 
827     // Ignore GEPs because they don't need promoting and the constant indices
828     // will prevent the transformation.
829     if (isa<GetElementPtrInst>(V))
830       return true;
831 
832     if (!isSupportedValue(V) || (shouldPromote(V) && !isLegalToPromote(V))) {
833       LLVM_DEBUG(dbgs() << "IR Promotion: Can't handle: " << *V << "\n");
834       return false;
835     }
836 
837     WorkList.insert(V);
838     return true;
839   };
840 
841   // Iterate through, and add to, a tree of operands and users in the use-def.
842   while (!WorkList.empty()) {
843     Value *V = WorkList.pop_back_val();
844     if (CurrentVisited.count(V))
845       continue;
846 
847     // Ignore non-instructions, other than arguments.
848     if (!isa<Instruction>(V) && !isSource(V))
849       continue;
850 
851     // If we've already visited this value from somewhere, bail now because
852     // the tree has already been explored.
853     // TODO: This could limit the transform, ie if we try to promote something
854     // from an i8 and fail first, before trying an i16.
855     if (AllVisited.count(V))
856       return false;
857 
858     CurrentVisited.insert(V);
859     AllVisited.insert(V);
860 
861     // Calls can be both sources and sinks.
862     if (isSink(V))
863       Sinks.insert(cast<Instruction>(V));
864 
865     if (isSource(V))
866       Sources.insert(V);
867 
868     if (!isSink(V) && !isSource(V)) {
869       if (auto *I = dyn_cast<Instruction>(V)) {
870         // Visit operands of any instruction visited.
871         for (auto &U : I->operands()) {
872           if (!AddLegalInst(U))
873             return false;
874         }
875       }
876     }
877 
878     // Don't visit users of a node which isn't going to be mutated unless its a
879     // source.
880     if (isSource(V) || shouldPromote(V)) {
881       for (Use &U : V->uses()) {
882         if (!AddLegalInst(U.getUser()))
883           return false;
884       }
885     }
886   }
887 
888   LLVM_DEBUG(dbgs() << "IR Promotion: Visited nodes:\n";
889              for (auto *I : CurrentVisited)
890                I->dump();
891              );
892 
893   unsigned ToPromote = 0;
894   unsigned NonFreeArgs = 0;
895   SmallPtrSet<BasicBlock*, 4> Blocks;
896   for (auto *V : CurrentVisited) {
897     if (auto *I = dyn_cast<Instruction>(V))
898       Blocks.insert(I->getParent());
899 
900     if (Sources.count(V)) {
901       if (auto *Arg = dyn_cast<Argument>(V))
902         if (!Arg->hasZExtAttr() && !Arg->hasSExtAttr())
903           ++NonFreeArgs;
904       continue;
905     }
906 
907     if (Sinks.count(cast<Instruction>(V)))
908       continue;
909      ++ToPromote;
910    }
911 
912   // DAG optimizations should be able to handle these cases better, especially
913   // for function arguments.
914   if (ToPromote < 2 || (Blocks.size() == 1 && (NonFreeArgs > SafeWrap.size())))
915     return false;
916 
917   IRPromoter Promoter(*Ctx, cast<IntegerType>(OrigTy), PromotedWidth,
918                       CurrentVisited, Sources, Sinks, SafeWrap);
919   Promoter.Mutate();
920   return true;
921 }
922 
923 bool TypePromotion::runOnFunction(Function &F) {
924   if (skipFunction(F) || DisablePromotion)
925     return false;
926 
927   LLVM_DEBUG(dbgs() << "IR Promotion: Running on " << F.getName() << "\n");
928 
929   auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
930   if (!TPC)
931     return false;
932 
933   AllVisited.clear();
934   SafeToPromote.clear();
935   SafeWrap.clear();
936   bool MadeChange = false;
937   const DataLayout &DL = F.getParent()->getDataLayout();
938   const TargetMachine &TM = TPC->getTM<TargetMachine>();
939   const TargetSubtargetInfo *SubtargetInfo = TM.getSubtargetImpl(F);
940   const TargetLowering *TLI = SubtargetInfo->getTargetLowering();
941   const TargetTransformInfo &TII =
942     getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
943   RegisterBitWidth =
944       TII.getRegisterBitWidth(TargetTransformInfo::RGK_Scalar).getFixedSize();
945   Ctx = &F.getParent()->getContext();
946 
947   // Search up from icmps to try to promote their operands.
948   for (BasicBlock &BB : F) {
949     for (auto &I : BB) {
950       if (AllVisited.count(&I))
951         continue;
952 
953       if (!isa<ICmpInst>(&I))
954         continue;
955 
956       auto *ICmp = cast<ICmpInst>(&I);
957       // Skip signed or pointer compares
958       if (ICmp->isSigned() ||
959           !isa<IntegerType>(ICmp->getOperand(0)->getType()))
960         continue;
961 
962       LLVM_DEBUG(dbgs() << "IR Promotion: Searching from: " << *ICmp << "\n");
963 
964       for (auto &Op : ICmp->operands()) {
965         if (auto *I = dyn_cast<Instruction>(Op)) {
966           EVT SrcVT = TLI->getValueType(DL, I->getType());
967           if (SrcVT.isSimple() && TLI->isTypeLegal(SrcVT.getSimpleVT()))
968             break;
969 
970           if (TLI->getTypeAction(ICmp->getContext(), SrcVT) !=
971               TargetLowering::TypePromoteInteger)
972             break;
973           EVT PromotedVT = TLI->getTypeToTransformTo(ICmp->getContext(), SrcVT);
974           if (RegisterBitWidth < PromotedVT.getFixedSizeInBits()) {
975             LLVM_DEBUG(dbgs() << "IR Promotion: Couldn't find target register "
976                        << "for promoted type\n");
977             break;
978           }
979 
980           MadeChange |= TryToPromote(I, PromotedVT.getFixedSizeInBits());
981           break;
982         }
983       }
984     }
985     LLVM_DEBUG(if (verifyFunction(F, &dbgs())) {
986                 dbgs() << F;
987                 report_fatal_error("Broken function after type promotion");
988                });
989   }
990   if (MadeChange)
991     LLVM_DEBUG(dbgs() << "After TypePromotion: " << F << "\n");
992 
993   AllVisited.clear();
994   SafeToPromote.clear();
995   SafeWrap.clear();
996 
997   return MadeChange;
998 }
999 
1000 INITIALIZE_PASS_BEGIN(TypePromotion, DEBUG_TYPE, PASS_NAME, false, false)
1001 INITIALIZE_PASS_END(TypePromotion, DEBUG_TYPE, PASS_NAME, false, false)
1002 
1003 char TypePromotion::ID = 0;
1004 
1005 FunctionPass *llvm::createTypePromotionPass() {
1006   return new TypePromotion();
1007 }
1008