| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 1 | //===----- ARMCodeGenPrepare.cpp ------------------------------------------===// | 
|  | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
|  | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
|  | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 | // | 
|  | 10 | /// \file | 
|  | 11 | /// This pass inserts intrinsics to handle small types that would otherwise be | 
|  | 12 | /// promoted during legalization. Here we can manually promote types or insert | 
|  | 13 | /// intrinsics which can handle narrow types that aren't supported by the | 
|  | 14 | /// register classes. | 
|  | 15 | // | 
|  | 16 | //===----------------------------------------------------------------------===// | 
|  | 17 |  | 
|  | 18 | #include "ARM.h" | 
|  | 19 | #include "ARMSubtarget.h" | 
|  | 20 | #include "ARMTargetMachine.h" | 
|  | 21 | #include "llvm/ADT/StringRef.h" | 
|  | 22 | #include "llvm/CodeGen/Passes.h" | 
|  | 23 | #include "llvm/CodeGen/TargetPassConfig.h" | 
|  | 24 | #include "llvm/IR/Attributes.h" | 
|  | 25 | #include "llvm/IR/BasicBlock.h" | 
|  | 26 | #include "llvm/IR/IRBuilder.h" | 
|  | 27 | #include "llvm/IR/Constants.h" | 
|  | 28 | #include "llvm/IR/InstrTypes.h" | 
|  | 29 | #include "llvm/IR/Instruction.h" | 
|  | 30 | #include "llvm/IR/Instructions.h" | 
|  | 31 | #include "llvm/IR/IntrinsicInst.h" | 
|  | 32 | #include "llvm/IR/Intrinsics.h" | 
|  | 33 | #include "llvm/IR/Type.h" | 
|  | 34 | #include "llvm/IR/Value.h" | 
|  | 35 | #include "llvm/IR/Verifier.h" | 
|  | 36 | #include "llvm/Pass.h" | 
|  | 37 | #include "llvm/Support/Casting.h" | 
|  | 38 | #include "llvm/Support/CommandLine.h" | 
|  | 39 |  | 
|  | 40 | #define DEBUG_TYPE "arm-codegenprepare" | 
|  | 41 |  | 
|  | 42 | using namespace llvm; | 
|  | 43 |  | 
|  | 44 | static cl::opt<bool> | 
| Hans Wennborg | 5555c00 | 2018-09-24 11:40:07 +0000 | [diff] [blame] | 45 | DisableCGP("arm-disable-cgp", cl::Hidden, cl::init(true), | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 46 | cl::desc("Disable ARM specific CodeGenPrepare pass")); | 
|  | 47 |  | 
|  | 48 | static cl::opt<bool> | 
|  | 49 | EnableDSP("arm-enable-scalar-dsp", cl::Hidden, cl::init(false), | 
|  | 50 | cl::desc("Use DSP instructions for scalar operations")); | 
|  | 51 |  | 
|  | 52 | static cl::opt<bool> | 
|  | 53 | EnableDSPWithImms("arm-enable-scalar-dsp-imms", cl::Hidden, cl::init(false), | 
|  | 54 | cl::desc("Use DSP instructions for scalar operations\ | 
|  | 55 | with immediate operands")); | 
|  | 56 |  | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 57 | // The goal of this pass is to enable more efficient code generation for | 
|  | 58 | // operations on narrow types (i.e. types with < 32-bits) and this is a | 
|  | 59 | // motivating IR code example: | 
|  | 60 | // | 
|  | 61 | //   define hidden i32 @cmp(i8 zeroext) { | 
|  | 62 | //     %2 = add i8 %0, -49 | 
|  | 63 | //     %3 = icmp ult i8 %2, 3 | 
|  | 64 | //     .. | 
|  | 65 | //   } | 
|  | 66 | // | 
|  | 67 | // The issue here is that i8 is type-legalized to i32 because i8 is not a | 
|  | 68 | // legal type. Thus, arithmetic is done in integer-precision, but then the | 
|  | 69 | // byte value is masked out as follows: | 
|  | 70 | // | 
|  | 71 | //   t19: i32 = add t4, Constant:i32<-49> | 
|  | 72 | //     t24: i32 = and t19, Constant:i32<255> | 
|  | 73 | // | 
|  | 74 | // Consequently, we generate code like this: | 
|  | 75 | // | 
|  | 76 | //   subs  r0, #49 | 
|  | 77 | //   uxtb  r1, r0 | 
|  | 78 | //   cmp r1, #3 | 
|  | 79 | // | 
|  | 80 | // This shows that masking out the byte value results in generation of | 
|  | 81 | // the UXTB instruction. This is not optimal as r0 already contains the byte | 
|  | 82 | // value we need, and so instead we can just generate: | 
|  | 83 | // | 
|  | 84 | //   sub.w r1, r0, #49 | 
|  | 85 | //   cmp r1, #3 | 
|  | 86 | // | 
|  | 87 | // We achieve this by type promoting the IR to i32 like so for this example: | 
|  | 88 | // | 
|  | 89 | //   define i32 @cmp(i8 zeroext %c) { | 
|  | 90 | //     %0 = zext i8 %c to i32 | 
|  | 91 | //     %c.off = add i32 %0, -49 | 
|  | 92 | //     %1 = icmp ult i32 %c.off, 3 | 
|  | 93 | //     .. | 
|  | 94 | //   } | 
|  | 95 | // | 
|  | 96 | // For this to be valid and legal, we need to prove that the i32 add is | 
|  | 97 | // producing the same value as the i8 addition, and that e.g. no overflow | 
|  | 98 | // happens. | 
|  | 99 | // | 
|  | 100 | // A brief sketch of the algorithm and some terminology. | 
|  | 101 | // We pattern match interesting IR patterns: | 
|  | 102 | // - which have "sources": instructions producing narrow values (i8, i16), and | 
|  | 103 | // - they have "sinks": instructions consuming these narrow values. | 
|  | 104 | // | 
|  | 105 | // We collect all instruction connecting sources and sinks in a worklist, so | 
|  | 106 | // that we can mutate these instruction and perform type promotion when it is | 
|  | 107 | // legal to do so. | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 108 |  | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 109 | namespace { | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 110 | class IRPromoter { | 
|  | 111 | SmallPtrSet<Value*, 8> NewInsts; | 
|  | 112 | SmallVector<Instruction*, 4> InstsToRemove; | 
|  | 113 | Module *M = nullptr; | 
|  | 114 | LLVMContext &Ctx; | 
|  | 115 |  | 
|  | 116 | public: | 
|  | 117 | IRPromoter(Module *M) : M(M), Ctx(M->getContext()) { } | 
|  | 118 |  | 
|  | 119 | void Cleanup() { | 
|  | 120 | for (auto *I : InstsToRemove) { | 
|  | 121 | LLVM_DEBUG(dbgs() << "ARM CGP: Removing " << *I << "\n"); | 
|  | 122 | I->dropAllReferences(); | 
|  | 123 | I->eraseFromParent(); | 
|  | 124 | } | 
|  | 125 | InstsToRemove.clear(); | 
|  | 126 | NewInsts.clear(); | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | void Mutate(Type *OrigTy, | 
|  | 130 | SmallPtrSetImpl<Value*> &Visited, | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 131 | SmallPtrSetImpl<Value*> &Sources, | 
|  | 132 | SmallPtrSetImpl<Instruction*> &Sinks); | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 133 | }; | 
|  | 134 |  | 
|  | 135 | class ARMCodeGenPrepare : public FunctionPass { | 
|  | 136 | const ARMSubtarget *ST = nullptr; | 
|  | 137 | IRPromoter *Promoter = nullptr; | 
|  | 138 | std::set<Value*> AllVisited; | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 139 |  | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 140 | bool isSupportedValue(Value *V); | 
|  | 141 | bool isLegalToPromote(Value *V); | 
|  | 142 | bool TryToPromote(Value *V); | 
|  | 143 |  | 
|  | 144 | public: | 
|  | 145 | static char ID; | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 146 | static unsigned TypeSize; | 
|  | 147 | Type *OrigTy = nullptr; | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 148 |  | 
|  | 149 | ARMCodeGenPrepare() : FunctionPass(ID) {} | 
|  | 150 |  | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 151 | void getAnalysisUsage(AnalysisUsage &AU) const override { | 
|  | 152 | AU.addRequired<TargetPassConfig>(); | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | StringRef getPassName() const override { return "ARM IR optimizations"; } | 
|  | 156 |  | 
|  | 157 | bool doInitialization(Module &M) override; | 
|  | 158 | bool runOnFunction(Function &F) override; | 
| Matt Morehouse | a70685f | 2018-07-23 17:00:45 +0000 | [diff] [blame] | 159 | bool doFinalization(Module &M) override; | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 160 | }; | 
|  | 161 |  | 
|  | 162 | } | 
|  | 163 |  | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 164 | static bool generateSignBits(Value *V) { | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 165 | if (!isa<Instruction>(V)) | 
|  | 166 | return false; | 
|  | 167 |  | 
|  | 168 | unsigned Opc = cast<Instruction>(V)->getOpcode(); | 
|  | 169 | return Opc == Instruction::AShr || Opc == Instruction::SDiv || | 
|  | 170 | Opc == Instruction::SRem; | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | /// Some instructions can use 8- and 16-bit operands, and we don't need to | 
|  | 174 | /// promote anything larger. We disallow booleans to make life easier when | 
|  | 175 | /// dealing with icmps but allow any other integer that is <= 16 bits. Void | 
|  | 176 | /// types are accepted so we can handle switches. | 
|  | 177 | static bool isSupportedType(Value *V) { | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 178 | Type *Ty = V->getType(); | 
| Sam Parker | 7def86b | 2018-08-15 07:52:35 +0000 | [diff] [blame] | 179 |  | 
|  | 180 | // Allow voids and pointers, these won't be promoted. | 
|  | 181 | if (Ty->isVoidTy() || Ty->isPointerTy()) | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 182 | return true; | 
|  | 183 |  | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 184 | if (auto *Ld = dyn_cast<LoadInst>(V)) | 
|  | 185 | Ty = cast<PointerType>(Ld->getPointerOperandType())->getElementType(); | 
|  | 186 |  | 
|  | 187 | const IntegerType *IntTy = dyn_cast<IntegerType>(Ty); | 
| Sam Parker | aaec3c6 | 2018-09-13 15:14:12 +0000 | [diff] [blame] | 188 | if (!IntTy) | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 189 | return false; | 
|  | 190 |  | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 191 | return IntTy->getBitWidth() == ARMCodeGenPrepare::TypeSize; | 
|  | 192 | } | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 193 |  | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 194 | /// Return true if the given value is a source in the use-def chain, producing | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 195 | /// a narrow (i8, i16) value. These values will be zext to start the promotion | 
|  | 196 | /// of the tree to i32. We guarantee that these won't populate the upper bits | 
|  | 197 | /// of the register. ZExt on the loads will be free, and the same for call | 
|  | 198 | /// return values because we only accept ones that guarantee a zeroext ret val. | 
|  | 199 | /// Many arguments will have the zeroext attribute too, so those would be free | 
|  | 200 | /// too. | 
|  | 201 | static bool isSource(Value *V) { | 
| Sam Parker | 7def86b | 2018-08-15 07:52:35 +0000 | [diff] [blame] | 202 | if (!isa<IntegerType>(V->getType())) | 
|  | 203 | return false; | 
| Volodymyr Sapsai | 703ab84 | 2018-09-18 00:11:55 +0000 | [diff] [blame] | 204 | // TODO Allow zext to be sources. | 
|  | 205 | if (isa<Argument>(V)) | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 206 | return true; | 
| Volodymyr Sapsai | 703ab84 | 2018-09-18 00:11:55 +0000 | [diff] [blame] | 207 | else if (isa<LoadInst>(V)) | 
|  | 208 | return true; | 
|  | 209 | else if (isa<BitCastInst>(V)) | 
|  | 210 | return true; | 
|  | 211 | else if (auto *Call = dyn_cast<CallInst>(V)) | 
|  | 212 | return Call->hasRetAttr(Attribute::AttrKind::ZExt); | 
|  | 213 | else if (auto *Trunc = dyn_cast<TruncInst>(V)) | 
|  | 214 | return isSupportedType(Trunc); | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 215 | return false; | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 216 | } | 
|  | 217 |  | 
|  | 218 | /// Return true if V will require any promoted values to be truncated for the | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 219 | /// the IR to remain valid. We can't mutate the value type of these | 
|  | 220 | /// instructions. | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 221 | static bool isSink(Value *V) { | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 222 | // TODO The truncate also isn't actually necessary because we would already | 
|  | 223 | // proved that the data value is kept within the range of the original data | 
|  | 224 | // type. | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 225 | auto UsesNarrowValue = [](Value *V) { | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 226 | return V->getType()->getScalarSizeInBits() == ARMCodeGenPrepare::TypeSize; | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 227 | }; | 
|  | 228 |  | 
|  | 229 | if (auto *Store = dyn_cast<StoreInst>(V)) | 
|  | 230 | return UsesNarrowValue(Store->getValueOperand()); | 
|  | 231 | if (auto *Return = dyn_cast<ReturnInst>(V)) | 
|  | 232 | return UsesNarrowValue(Return->getReturnValue()); | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 233 | if (auto *Trunc = dyn_cast<TruncInst>(V)) | 
|  | 234 | return UsesNarrowValue(Trunc->getOperand(0)); | 
| Sam Parker | 0e2f0bd | 2018-08-16 11:54:09 +0000 | [diff] [blame] | 235 | if (auto *ZExt = dyn_cast<ZExtInst>(V)) | 
|  | 236 | return UsesNarrowValue(ZExt->getOperand(0)); | 
| Sam Parker | 13567db | 2018-08-16 10:05:39 +0000 | [diff] [blame] | 237 | if (auto *ICmp = dyn_cast<ICmpInst>(V)) | 
|  | 238 | return ICmp->isSigned(); | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 239 |  | 
|  | 240 | return isa<CallInst>(V); | 
|  | 241 | } | 
|  | 242 |  | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 243 | /// Return whether the instruction can be promoted within any modifications to | 
|  | 244 | /// it's operands or result. | 
|  | 245 | static bool isSafeOverflow(Instruction *I) { | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 246 | // FIXME Do we need NSW too? | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 247 | if (isa<OverflowingBinaryOperator>(I) && I->hasNoUnsignedWrap()) | 
|  | 248 | return true; | 
|  | 249 |  | 
|  | 250 | unsigned Opc = I->getOpcode(); | 
|  | 251 | if (Opc == Instruction::Add || Opc == Instruction::Sub) { | 
|  | 252 | // We don't care if the add or sub could wrap if the value is decreasing | 
|  | 253 | // and is only being used by an unsigned compare. | 
|  | 254 | if (!I->hasOneUse() || | 
|  | 255 | !isa<ICmpInst>(*I->user_begin()) || | 
|  | 256 | !isa<ConstantInt>(I->getOperand(1))) | 
|  | 257 | return false; | 
|  | 258 |  | 
|  | 259 | auto *CI = cast<ICmpInst>(*I->user_begin()); | 
| Sam Parker | 76d25d7 | 2018-09-17 13:48:25 +0000 | [diff] [blame] | 260 |  | 
|  | 261 | // Don't support an icmp that deals with sign bits, including negative | 
|  | 262 | // immediates | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 263 | if (CI->isSigned()) | 
|  | 264 | return false; | 
|  | 265 |  | 
| Sam Parker | 76d25d7 | 2018-09-17 13:48:25 +0000 | [diff] [blame] | 266 | if (auto *Const = dyn_cast<ConstantInt>(CI->getOperand(0))) | 
|  | 267 | if (Const->isNegative()) | 
|  | 268 | return false; | 
|  | 269 |  | 
|  | 270 | if (auto *Const = dyn_cast<ConstantInt>(CI->getOperand(1))) | 
|  | 271 | if (Const->isNegative()) | 
|  | 272 | return false; | 
|  | 273 |  | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 274 | bool NegImm = cast<ConstantInt>(I->getOperand(1))->isNegative(); | 
|  | 275 | bool IsDecreasing = ((Opc == Instruction::Sub) && !NegImm) || | 
|  | 276 | ((Opc == Instruction::Add) && NegImm); | 
|  | 277 | if (!IsDecreasing) | 
|  | 278 | return false; | 
|  | 279 |  | 
|  | 280 | LLVM_DEBUG(dbgs() << "ARM CGP: Allowing safe overflow for " << *I << "\n"); | 
|  | 281 | return true; | 
|  | 282 | } | 
|  | 283 |  | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 284 | return false; | 
|  | 285 | } | 
|  | 286 |  | 
|  | 287 | static bool shouldPromote(Value *V) { | 
| Sam Parker | aaec3c6 | 2018-09-13 15:14:12 +0000 | [diff] [blame] | 288 | if (!isa<IntegerType>(V->getType()) || isSink(V)) | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 289 | return false; | 
|  | 290 |  | 
|  | 291 | if (isSource(V)) | 
|  | 292 | return true; | 
|  | 293 |  | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 294 | auto *I = dyn_cast<Instruction>(V); | 
|  | 295 | if (!I) | 
|  | 296 | return false; | 
|  | 297 |  | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 298 | if (isa<ICmpInst>(I)) | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 299 | return false; | 
|  | 300 |  | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 301 | return true; | 
|  | 302 | } | 
|  | 303 |  | 
|  | 304 | /// Return whether we can safely mutate V's type to ExtTy without having to be | 
|  | 305 | /// concerned with zero extending or truncation. | 
|  | 306 | static bool isPromotedResultSafe(Value *V) { | 
|  | 307 | if (!isa<Instruction>(V)) | 
|  | 308 | return true; | 
|  | 309 |  | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 310 | if (generateSignBits(V)) | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 311 | return false; | 
|  | 312 |  | 
|  | 313 | // If I is only being used by something that will require its value to be | 
|  | 314 | // truncated, then we don't care about the promoted result. | 
|  | 315 | auto *I = cast<Instruction>(V); | 
|  | 316 | if (I->hasOneUse() && isSink(*I->use_begin())) | 
|  | 317 | return true; | 
|  | 318 |  | 
|  | 319 | if (isa<OverflowingBinaryOperator>(I)) | 
|  | 320 | return isSafeOverflow(I); | 
|  | 321 | return true; | 
|  | 322 | } | 
|  | 323 |  | 
|  | 324 | /// Return the intrinsic for the instruction that can perform the same | 
|  | 325 | /// operation but on a narrow type. This is using the parallel dsp intrinsics | 
|  | 326 | /// on scalar values. | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 327 | static Intrinsic::ID getNarrowIntrinsic(Instruction *I) { | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 328 | // Whether we use the signed or unsigned versions of these intrinsics | 
|  | 329 | // doesn't matter because we're not using the GE bits that they set in | 
|  | 330 | // the APSR. | 
|  | 331 | switch(I->getOpcode()) { | 
|  | 332 | default: | 
|  | 333 | break; | 
|  | 334 | case Instruction::Add: | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 335 | return ARMCodeGenPrepare::TypeSize == 16 ? Intrinsic::arm_uadd16 : | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 336 | Intrinsic::arm_uadd8; | 
|  | 337 | case Instruction::Sub: | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 338 | return ARMCodeGenPrepare::TypeSize == 16 ? Intrinsic::arm_usub16 : | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 339 | Intrinsic::arm_usub8; | 
|  | 340 | } | 
|  | 341 | llvm_unreachable("unhandled opcode for narrow intrinsic"); | 
|  | 342 | } | 
|  | 343 |  | 
|  | 344 | void IRPromoter::Mutate(Type *OrigTy, | 
|  | 345 | SmallPtrSetImpl<Value*> &Visited, | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 346 | SmallPtrSetImpl<Value*> &Sources, | 
|  | 347 | SmallPtrSetImpl<Instruction*> &Sinks) { | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 348 | IRBuilder<> Builder{Ctx}; | 
|  | 349 | Type *ExtTy = Type::getInt32Ty(M->getContext()); | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 350 | SmallPtrSet<Value*, 8> Promoted; | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 351 | LLVM_DEBUG(dbgs() << "ARM CGP: Promoting use-def chains to from " | 
|  | 352 | << ARMCodeGenPrepare::TypeSize << " to 32-bits\n"); | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 353 |  | 
| Sam Parker | 13567db | 2018-08-16 10:05:39 +0000 | [diff] [blame] | 354 | // Cache original types. | 
|  | 355 | DenseMap<Value*, Type*> TruncTysMap; | 
|  | 356 | for (auto *V : Visited) | 
|  | 357 | TruncTysMap[V] = V->getType(); | 
|  | 358 |  | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 359 | auto ReplaceAllUsersOfWith = [&](Value *From, Value *To) { | 
|  | 360 | SmallVector<Instruction*, 4> Users; | 
|  | 361 | Instruction *InstTo = dyn_cast<Instruction>(To); | 
|  | 362 | for (Use &U : From->uses()) { | 
|  | 363 | auto *User = cast<Instruction>(U.getUser()); | 
|  | 364 | if (InstTo && User->isIdenticalTo(InstTo)) | 
|  | 365 | continue; | 
|  | 366 | Users.push_back(User); | 
|  | 367 | } | 
|  | 368 |  | 
| Sam Parker | aaec3c6 | 2018-09-13 15:14:12 +0000 | [diff] [blame] | 369 | for (auto *U : Users) | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 370 | U->replaceUsesOfWith(From, To); | 
|  | 371 | }; | 
|  | 372 |  | 
|  | 373 | auto FixConst = [&](ConstantInt *Const, Instruction *I) { | 
| Sam Parker | 96f77f1 | 2018-09-13 14:48:10 +0000 | [diff] [blame] | 374 | Constant *NewConst = isSafeOverflow(I) && Const->isNegative() ? | 
|  | 375 | ConstantExpr::getSExt(Const, ExtTy) : | 
|  | 376 | ConstantExpr::getZExt(Const, ExtTy); | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 377 | I->replaceUsesOfWith(Const, NewConst); | 
|  | 378 | }; | 
|  | 379 |  | 
|  | 380 | auto InsertDSPIntrinsic = [&](Instruction *I) { | 
|  | 381 | LLVM_DEBUG(dbgs() << "ARM CGP: Inserting DSP intrinsic for " | 
|  | 382 | << *I << "\n"); | 
|  | 383 | Function *DSPInst = | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 384 | Intrinsic::getDeclaration(M, getNarrowIntrinsic(I)); | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 385 | Builder.SetInsertPoint(I); | 
|  | 386 | Builder.SetCurrentDebugLocation(I->getDebugLoc()); | 
|  | 387 | Value *Args[] = { I->getOperand(0), I->getOperand(1) }; | 
|  | 388 | CallInst *Call = Builder.CreateCall(DSPInst, Args); | 
|  | 389 | ReplaceAllUsersOfWith(I, Call); | 
|  | 390 | InstsToRemove.push_back(I); | 
|  | 391 | NewInsts.insert(Call); | 
| Sam Parker | 13567db | 2018-08-16 10:05:39 +0000 | [diff] [blame] | 392 | TruncTysMap[Call] = OrigTy; | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 393 | }; | 
|  | 394 |  | 
|  | 395 | auto InsertZExt = [&](Value *V, Instruction *InsertPt) { | 
|  | 396 | LLVM_DEBUG(dbgs() << "ARM CGP: Inserting ZExt for " << *V << "\n"); | 
|  | 397 | Builder.SetInsertPoint(InsertPt); | 
|  | 398 | if (auto *I = dyn_cast<Instruction>(V)) | 
|  | 399 | Builder.SetCurrentDebugLocation(I->getDebugLoc()); | 
|  | 400 | auto *ZExt = cast<Instruction>(Builder.CreateZExt(V, ExtTy)); | 
|  | 401 | if (isa<Argument>(V)) | 
|  | 402 | ZExt->moveBefore(InsertPt); | 
|  | 403 | else | 
|  | 404 | ZExt->moveAfter(InsertPt); | 
|  | 405 | ReplaceAllUsersOfWith(V, ZExt); | 
|  | 406 | NewInsts.insert(ZExt); | 
| Sam Parker | 13567db | 2018-08-16 10:05:39 +0000 | [diff] [blame] | 407 | TruncTysMap[ZExt] = TruncTysMap[V]; | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 408 | }; | 
|  | 409 |  | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 410 | // First, insert extending instructions between the sources and their users. | 
|  | 411 | LLVM_DEBUG(dbgs() << "ARM CGP: Promoting sources:\n"); | 
|  | 412 | for (auto V : Sources) { | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 413 | LLVM_DEBUG(dbgs() << " - " << *V << "\n"); | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 414 | if (auto *I = dyn_cast<Instruction>(V)) | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 415 | InsertZExt(I, I); | 
|  | 416 | else if (auto *Arg = dyn_cast<Argument>(V)) { | 
|  | 417 | BasicBlock &BB = Arg->getParent()->front(); | 
|  | 418 | InsertZExt(Arg, &*BB.getFirstInsertionPt()); | 
|  | 419 | } else { | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 420 | llvm_unreachable("unhandled source that needs extending"); | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 421 | } | 
|  | 422 | Promoted.insert(V); | 
|  | 423 | } | 
|  | 424 |  | 
|  | 425 | LLVM_DEBUG(dbgs() << "ARM CGP: Mutating the tree..\n"); | 
|  | 426 | // Then mutate the types of the instructions within the tree. Here we handle | 
|  | 427 | // constant operands. | 
|  | 428 | for (auto *V : Visited) { | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 429 | if (Sources.count(V)) | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 430 | continue; | 
|  | 431 |  | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 432 | auto *I = cast<Instruction>(V); | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 433 | if (Sinks.count(I)) | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 434 | continue; | 
|  | 435 |  | 
| Sam Parker | 7def86b | 2018-08-15 07:52:35 +0000 | [diff] [blame] | 436 | for (unsigned i = 0, e = I->getNumOperands(); i < e; ++i) { | 
|  | 437 | Value *Op = I->getOperand(i); | 
|  | 438 | if ((Op->getType() == ExtTy) || !isa<IntegerType>(Op->getType())) | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 439 | continue; | 
|  | 440 |  | 
| Sam Parker | 7def86b | 2018-08-15 07:52:35 +0000 | [diff] [blame] | 441 | if (auto *Const = dyn_cast<ConstantInt>(Op)) | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 442 | FixConst(Const, I); | 
| Sam Parker | 7def86b | 2018-08-15 07:52:35 +0000 | [diff] [blame] | 443 | else if (isa<UndefValue>(Op)) | 
|  | 444 | I->setOperand(i, UndefValue::get(ExtTy)); | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 445 | } | 
|  | 446 |  | 
|  | 447 | if (shouldPromote(I)) { | 
|  | 448 | I->mutateType(ExtTy); | 
|  | 449 | Promoted.insert(I); | 
|  | 450 | } | 
|  | 451 | } | 
|  | 452 |  | 
|  | 453 | // Now we need to remove any zexts that have become unnecessary, as well | 
|  | 454 | // as insert any intrinsics. | 
|  | 455 | for (auto *V : Visited) { | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 456 | if (Sources.count(V)) | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 457 | continue; | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 458 |  | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 459 | if (!shouldPromote(V) || isPromotedResultSafe(V)) | 
|  | 460 | continue; | 
|  | 461 |  | 
|  | 462 | // Replace unsafe instructions with appropriate intrinsic calls. | 
|  | 463 | InsertDSPIntrinsic(cast<Instruction>(V)); | 
|  | 464 | } | 
|  | 465 |  | 
| Sam Parker | 13567db | 2018-08-16 10:05:39 +0000 | [diff] [blame] | 466 | auto InsertTrunc = [&](Value *V) -> Instruction* { | 
|  | 467 | if (!isa<Instruction>(V) || !isa<IntegerType>(V->getType())) | 
|  | 468 | return nullptr; | 
|  | 469 |  | 
| Sam Parker | 0e2f0bd | 2018-08-16 11:54:09 +0000 | [diff] [blame] | 470 | if ((!Promoted.count(V) && !NewInsts.count(V)) || !TruncTysMap.count(V) || | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 471 | Sources.count(V)) | 
| Sam Parker | 13567db | 2018-08-16 10:05:39 +0000 | [diff] [blame] | 472 | return nullptr; | 
|  | 473 |  | 
|  | 474 | Type *TruncTy = TruncTysMap[V]; | 
|  | 475 | if (TruncTy == ExtTy) | 
|  | 476 | return nullptr; | 
|  | 477 |  | 
|  | 478 | LLVM_DEBUG(dbgs() << "ARM CGP: Creating " << *TruncTy << " Trunc for " | 
|  | 479 | << *V << "\n"); | 
|  | 480 | Builder.SetInsertPoint(cast<Instruction>(V)); | 
|  | 481 | auto *Trunc = cast<Instruction>(Builder.CreateTrunc(V, TruncTy)); | 
|  | 482 | NewInsts.insert(Trunc); | 
|  | 483 | return Trunc; | 
|  | 484 | }; | 
|  | 485 |  | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 486 | LLVM_DEBUG(dbgs() << "ARM CGP: Fixing up the sinks:\n"); | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 487 | // Fix up any stores or returns that use the results of the promoted | 
|  | 488 | // chain. | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 489 | for (auto I : Sinks) { | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 490 | LLVM_DEBUG(dbgs() << " - " << *I << "\n"); | 
| Sam Parker | 13567db | 2018-08-16 10:05:39 +0000 | [diff] [blame] | 491 |  | 
|  | 492 | // Handle calls separately as we need to iterate over arg operands. | 
|  | 493 | if (auto *Call = dyn_cast<CallInst>(I)) { | 
|  | 494 | for (unsigned i = 0; i < Call->getNumArgOperands(); ++i) { | 
|  | 495 | Value *Arg = Call->getArgOperand(i); | 
|  | 496 | if (Instruction *Trunc = InsertTrunc(Arg)) { | 
|  | 497 | Trunc->moveBefore(Call); | 
|  | 498 | Call->setArgOperand(i, Trunc); | 
|  | 499 | } | 
|  | 500 | } | 
|  | 501 | continue; | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 502 | } | 
|  | 503 |  | 
| Sam Parker | 13567db | 2018-08-16 10:05:39 +0000 | [diff] [blame] | 504 | // Now handle the others. | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 505 | for (unsigned i = 0; i < I->getNumOperands(); ++i) { | 
| Sam Parker | 13567db | 2018-08-16 10:05:39 +0000 | [diff] [blame] | 506 | if (Instruction *Trunc = InsertTrunc(I->getOperand(i))) { | 
|  | 507 | Trunc->moveBefore(I); | 
|  | 508 | I->setOperand(i, Trunc); | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 509 | } | 
|  | 510 | } | 
|  | 511 | } | 
| Sam Parker | 0e2f0bd | 2018-08-16 11:54:09 +0000 | [diff] [blame] | 512 | LLVM_DEBUG(dbgs() << "ARM CGP: Mutation complete:\n"); | 
| Sam Parker | aaec3c6 | 2018-09-13 15:14:12 +0000 | [diff] [blame] | 513 | LLVM_DEBUG(dbgs(); | 
|  | 514 | for (auto *V : Sources) | 
|  | 515 | V->dump(); | 
|  | 516 | for (auto *I : NewInsts) | 
|  | 517 | I->dump(); | 
|  | 518 | for (auto *V : Visited) { | 
|  | 519 | if (!Sources.count(V)) | 
|  | 520 | V->dump(); | 
|  | 521 | }); | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 522 | } | 
|  | 523 |  | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 524 | /// We accept most instructions, as well as Arguments and ConstantInsts. We | 
|  | 525 | /// Disallow casts other than zext and truncs and only allow calls if their | 
|  | 526 | /// return value is zeroext. We don't allow opcodes that can introduce sign | 
|  | 527 | /// bits. | 
|  | 528 | bool ARMCodeGenPrepare::isSupportedValue(Value *V) { | 
| Sam Parker | 13567db | 2018-08-16 10:05:39 +0000 | [diff] [blame] | 529 | if (isa<ICmpInst>(V)) | 
|  | 530 | return true; | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 531 |  | 
| Volodymyr Sapsai | 703ab84 | 2018-09-18 00:11:55 +0000 | [diff] [blame] | 532 | // Memory instructions | 
|  | 533 | if (isa<StoreInst>(V) || isa<GetElementPtrInst>(V)) | 
|  | 534 | return true; | 
|  | 535 |  | 
|  | 536 | // Branches and targets. | 
|  | 537 | if( isa<BranchInst>(V) || isa<SwitchInst>(V) || isa<BasicBlock>(V)) | 
|  | 538 | return true; | 
|  | 539 |  | 
|  | 540 | // Non-instruction values that we can handle. | 
|  | 541 | if ((isa<Constant>(V) && !isa<ConstantExpr>(V)) || isa<Argument>(V)) | 
|  | 542 | return isSupportedType(V); | 
|  | 543 |  | 
|  | 544 | if (isa<PHINode>(V) || isa<SelectInst>(V) || isa<ReturnInst>(V) || | 
|  | 545 | isa<LoadInst>(V)) | 
|  | 546 | return isSupportedType(V); | 
|  | 547 |  | 
|  | 548 | // Truncs can be either sources or sinks. | 
|  | 549 | if (auto *Trunc = dyn_cast<TruncInst>(V)) | 
|  | 550 | return isSupportedType(Trunc) || isSupportedType(Trunc->getOperand(0)); | 
|  | 551 |  | 
|  | 552 | if (isa<CastInst>(V) && !isa<SExtInst>(V)) | 
|  | 553 | return isSupportedType(cast<CastInst>(V)->getOperand(0)); | 
| Sam Parker | 0e2f0bd | 2018-08-16 11:54:09 +0000 | [diff] [blame] | 554 |  | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 555 | // Special cases for calls as we need to check for zeroext | 
|  | 556 | // TODO We should accept calls even if they don't have zeroext, as they can | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 557 | // still be sinks. | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 558 | if (auto *Call = dyn_cast<CallInst>(V)) | 
|  | 559 | return isSupportedType(Call) && | 
|  | 560 | Call->hasRetAttr(Attribute::AttrKind::ZExt); | 
|  | 561 |  | 
| Volodymyr Sapsai | 703ab84 | 2018-09-18 00:11:55 +0000 | [diff] [blame] | 562 | if (!isa<BinaryOperator>(V)) | 
|  | 563 | return false; | 
|  | 564 |  | 
|  | 565 | if (!isSupportedType(V)) | 
|  | 566 | return false; | 
|  | 567 |  | 
|  | 568 | if (generateSignBits(V)) { | 
|  | 569 | LLVM_DEBUG(dbgs() << "ARM CGP: No, instruction can generate sign bits.\n"); | 
|  | 570 | return false; | 
|  | 571 | } | 
|  | 572 | return true; | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 573 | } | 
|  | 574 |  | 
|  | 575 | /// Check that the type of V would be promoted and that the original type is | 
|  | 576 | /// smaller than the targeted promoted type. Check that we're not trying to | 
|  | 577 | /// promote something larger than our base 'TypeSize' type. | 
|  | 578 | bool ARMCodeGenPrepare::isLegalToPromote(Value *V) { | 
|  | 579 | if (isPromotedResultSafe(V)) | 
|  | 580 | return true; | 
|  | 581 |  | 
|  | 582 | auto *I = dyn_cast<Instruction>(V); | 
|  | 583 | if (!I) | 
|  | 584 | return false; | 
|  | 585 |  | 
|  | 586 | // If promotion is not safe, can we use a DSP instruction to natively | 
|  | 587 | // handle the narrow type? | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 588 | if (!ST->hasDSP() || !EnableDSP || !isSupportedType(I)) | 
|  | 589 | return false; | 
|  | 590 |  | 
|  | 591 | if (ST->isThumb() && !ST->hasThumb2()) | 
|  | 592 | return false; | 
|  | 593 |  | 
|  | 594 | if (I->getOpcode() != Instruction::Add && I->getOpcode() != Instruction::Sub) | 
|  | 595 | return false; | 
|  | 596 |  | 
|  | 597 | // TODO | 
|  | 598 | // Would it be profitable? For Thumb code, these parallel DSP instructions | 
|  | 599 | // are only Thumb-2, so we wouldn't be able to dual issue on Cortex-M33. For | 
|  | 600 | // Cortex-A, specifically Cortex-A72, the latency is double and throughput is | 
|  | 601 | // halved. They also do not take immediates as operands. | 
|  | 602 | for (auto &Op : I->operands()) { | 
|  | 603 | if (isa<Constant>(Op)) { | 
|  | 604 | if (!EnableDSPWithImms) | 
|  | 605 | return false; | 
|  | 606 | } | 
|  | 607 | } | 
|  | 608 | return true; | 
|  | 609 | } | 
|  | 610 |  | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 611 | bool ARMCodeGenPrepare::TryToPromote(Value *V) { | 
|  | 612 | OrigTy = V->getType(); | 
|  | 613 | TypeSize = OrigTy->getPrimitiveSizeInBits(); | 
| Sam Parker | fabf7fe | 2018-08-15 13:29:50 +0000 | [diff] [blame] | 614 | if (TypeSize > 16 || TypeSize < 8) | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 615 | return false; | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 616 |  | 
|  | 617 | if (!isSupportedValue(V) || !shouldPromote(V) || !isLegalToPromote(V)) | 
|  | 618 | return false; | 
|  | 619 |  | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 620 | LLVM_DEBUG(dbgs() << "ARM CGP: TryToPromote: " << *V << ", TypeSize = " | 
|  | 621 | << TypeSize << "\n"); | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 622 |  | 
|  | 623 | SetVector<Value*> WorkList; | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 624 | SmallPtrSet<Value*, 8> Sources; | 
|  | 625 | SmallPtrSet<Instruction*, 4> Sinks; | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 626 | WorkList.insert(V); | 
|  | 627 | SmallPtrSet<Value*, 16> CurrentVisited; | 
|  | 628 | CurrentVisited.clear(); | 
|  | 629 |  | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 630 | // Return true if V was added to the worklist as a supported instruction, | 
|  | 631 | // if it was already visited, or if we don't need to explore it (e.g. | 
|  | 632 | // pointer values and GEPs), and false otherwise. | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 633 | auto AddLegalInst = [&](Value *V) { | 
|  | 634 | if (CurrentVisited.count(V)) | 
|  | 635 | return true; | 
|  | 636 |  | 
| Sam Parker | 0d51197 | 2018-08-16 12:24:40 +0000 | [diff] [blame] | 637 | // Ignore GEPs because they don't need promoting and the constant indices | 
|  | 638 | // will prevent the transformation. | 
|  | 639 | if (isa<GetElementPtrInst>(V)) | 
|  | 640 | return true; | 
|  | 641 |  | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 642 | if (!isSupportedValue(V) || (shouldPromote(V) && !isLegalToPromote(V))) { | 
|  | 643 | LLVM_DEBUG(dbgs() << "ARM CGP: Can't handle: " << *V << "\n"); | 
|  | 644 | return false; | 
|  | 645 | } | 
|  | 646 |  | 
|  | 647 | WorkList.insert(V); | 
|  | 648 | return true; | 
|  | 649 | }; | 
|  | 650 |  | 
|  | 651 | // Iterate through, and add to, a tree of operands and users in the use-def. | 
|  | 652 | while (!WorkList.empty()) { | 
|  | 653 | Value *V = WorkList.back(); | 
|  | 654 | WorkList.pop_back(); | 
|  | 655 | if (CurrentVisited.count(V)) | 
|  | 656 | continue; | 
|  | 657 |  | 
| Sam Parker | 7def86b | 2018-08-15 07:52:35 +0000 | [diff] [blame] | 658 | // Ignore non-instructions, other than arguments. | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 659 | if (!isa<Instruction>(V) && !isSource(V)) | 
|  | 660 | continue; | 
|  | 661 |  | 
|  | 662 | // If we've already visited this value from somewhere, bail now because | 
|  | 663 | // the tree has already been explored. | 
|  | 664 | // TODO: This could limit the transform, ie if we try to promote something | 
|  | 665 | // from an i8 and fail first, before trying an i16. | 
| Sam Parker | aaec3c6 | 2018-09-13 15:14:12 +0000 | [diff] [blame] | 666 | if (AllVisited.count(V)) | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 667 | return false; | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 668 |  | 
|  | 669 | CurrentVisited.insert(V); | 
|  | 670 | AllVisited.insert(V); | 
|  | 671 |  | 
|  | 672 | // Calls can be both sources and sinks. | 
|  | 673 | if (isSink(V)) | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 674 | Sinks.insert(cast<Instruction>(V)); | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 675 | if (isSource(V)) | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 676 | Sources.insert(V); | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 677 | else if (auto *I = dyn_cast<Instruction>(V)) { | 
|  | 678 | // Visit operands of any instruction visited. | 
|  | 679 | for (auto &U : I->operands()) { | 
|  | 680 | if (!AddLegalInst(U)) | 
|  | 681 | return false; | 
|  | 682 | } | 
|  | 683 | } | 
|  | 684 |  | 
|  | 685 | // Don't visit users of a node which isn't going to be mutated unless its a | 
|  | 686 | // source. | 
|  | 687 | if (isSource(V) || shouldPromote(V)) { | 
|  | 688 | for (Use &U : V->uses()) { | 
|  | 689 | if (!AddLegalInst(U.getUser())) | 
|  | 690 | return false; | 
|  | 691 | } | 
|  | 692 | } | 
|  | 693 | } | 
|  | 694 |  | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 695 | LLVM_DEBUG(dbgs() << "ARM CGP: Visited nodes:\n"; | 
|  | 696 | for (auto *I : CurrentVisited) | 
|  | 697 | I->dump(); | 
|  | 698 | ); | 
| Sam Parker | 7def86b | 2018-08-15 07:52:35 +0000 | [diff] [blame] | 699 | unsigned ToPromote = 0; | 
|  | 700 | for (auto *V : CurrentVisited) { | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 701 | if (Sources.count(V)) | 
| Sam Parker | 7def86b | 2018-08-15 07:52:35 +0000 | [diff] [blame] | 702 | continue; | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 703 | if (Sinks.count(cast<Instruction>(V))) | 
| Sam Parker | 7def86b | 2018-08-15 07:52:35 +0000 | [diff] [blame] | 704 | continue; | 
|  | 705 | ++ToPromote; | 
|  | 706 | } | 
|  | 707 |  | 
|  | 708 | if (ToPromote < 2) | 
|  | 709 | return false; | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 710 |  | 
| Sjoerd Meijer | 31239a4 | 2018-08-17 07:34:01 +0000 | [diff] [blame] | 711 | Promoter->Mutate(OrigTy, CurrentVisited, Sources, Sinks); | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 712 | return true; | 
|  | 713 | } | 
|  | 714 |  | 
|  | 715 | bool ARMCodeGenPrepare::doInitialization(Module &M) { | 
|  | 716 | Promoter = new IRPromoter(&M); | 
|  | 717 | return false; | 
|  | 718 | } | 
|  | 719 |  | 
|  | 720 | bool ARMCodeGenPrepare::runOnFunction(Function &F) { | 
|  | 721 | if (skipFunction(F) || DisableCGP) | 
|  | 722 | return false; | 
|  | 723 |  | 
|  | 724 | auto *TPC = &getAnalysis<TargetPassConfig>(); | 
|  | 725 | if (!TPC) | 
|  | 726 | return false; | 
|  | 727 |  | 
|  | 728 | const TargetMachine &TM = TPC->getTM<TargetMachine>(); | 
|  | 729 | ST = &TM.getSubtarget<ARMSubtarget>(F); | 
|  | 730 | bool MadeChange = false; | 
|  | 731 | LLVM_DEBUG(dbgs() << "ARM CGP: Running on " << F.getName() << "\n"); | 
|  | 732 |  | 
|  | 733 | // Search up from icmps to try to promote their operands. | 
|  | 734 | for (BasicBlock &BB : F) { | 
|  | 735 | auto &Insts = BB.getInstList(); | 
|  | 736 | for (auto &I : Insts) { | 
|  | 737 | if (AllVisited.count(&I)) | 
|  | 738 | continue; | 
|  | 739 |  | 
|  | 740 | if (isa<ICmpInst>(I)) { | 
|  | 741 | auto &CI = cast<ICmpInst>(I); | 
|  | 742 |  | 
|  | 743 | // Skip signed or pointer compares | 
|  | 744 | if (CI.isSigned() || !isa<IntegerType>(CI.getOperand(0)->getType())) | 
|  | 745 | continue; | 
|  | 746 |  | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 747 | for (auto &Op : CI.operands()) { | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 748 | if (auto *I = dyn_cast<Instruction>(Op)) | 
|  | 749 | MadeChange |= TryToPromote(I); | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 750 | } | 
|  | 751 | } | 
|  | 752 | } | 
|  | 753 | Promoter->Cleanup(); | 
|  | 754 | LLVM_DEBUG(if (verifyFunction(F, &dbgs())) { | 
|  | 755 | dbgs(); | 
|  | 756 | report_fatal_error("Broken function after type promotion"); | 
|  | 757 | }); | 
|  | 758 | } | 
|  | 759 | if (MadeChange) | 
|  | 760 | LLVM_DEBUG(dbgs() << "After ARMCodeGenPrepare: " << F << "\n"); | 
|  | 761 |  | 
|  | 762 | return MadeChange; | 
|  | 763 | } | 
|  | 764 |  | 
| Matt Morehouse | a70685f | 2018-07-23 17:00:45 +0000 | [diff] [blame] | 765 | bool ARMCodeGenPrepare::doFinalization(Module &M) { | 
|  | 766 | delete Promoter; | 
|  | 767 | return false; | 
|  | 768 | } | 
|  | 769 |  | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 770 | INITIALIZE_PASS_BEGIN(ARMCodeGenPrepare, DEBUG_TYPE, | 
|  | 771 | "ARM IR optimizations", false, false) | 
|  | 772 | INITIALIZE_PASS_END(ARMCodeGenPrepare, DEBUG_TYPE, "ARM IR optimizations", | 
|  | 773 | false, false) | 
|  | 774 |  | 
|  | 775 | char ARMCodeGenPrepare::ID = 0; | 
| Sam Parker | 8c4b964 | 2018-08-10 13:57:13 +0000 | [diff] [blame] | 776 | unsigned ARMCodeGenPrepare::TypeSize = 0; | 
| Sam Parker | 3828c6f | 2018-07-23 12:27:47 +0000 | [diff] [blame] | 777 |  | 
|  | 778 | FunctionPass *llvm::createARMCodeGenPreparePass() { | 
|  | 779 | return new ARMCodeGenPrepare(); | 
|  | 780 | } |