Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 1 | //===- PPCBoolRetToInt.cpp ------------------------------------------------===// |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Guozhi Wei | f31c56d | 2017-06-08 18:27:24 +0000 | [diff] [blame] | 9 | // This file implements converting i1 values to i32/i64 if they could be more |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 10 | // profitably allocated as GPRs rather than CRs. This pass will become totally |
| 11 | // unnecessary if Register Bank Allocation and Global Instruction Selection ever |
| 12 | // go upstream. |
| 13 | // |
Guozhi Wei | f31c56d | 2017-06-08 18:27:24 +0000 | [diff] [blame] | 14 | // Presently, the pass converts i1 Constants, and Arguments to i32/i64 if the |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 15 | // transitive closure of their uses includes only PHINodes, CallInsts, and |
| 16 | // ReturnInsts. The rational is that arguments are generally passed and returned |
Guozhi Wei | f31c56d | 2017-06-08 18:27:24 +0000 | [diff] [blame] | 17 | // in GPRs rather than CRs, so casting them to i32/i64 at the LLVM IR level will |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 18 | // actually save casts at the Machine Instruction level. |
| 19 | // |
| 20 | // It might be useful to expand this pass to add bit-wise operations to the list |
| 21 | // of safe transitive closure types. Also, we miss some opportunities when LLVM |
| 22 | // represents logical AND and OR operations with control flow rather than data |
| 23 | // flow. For example by lowering the expression: return (A && B && C) |
| 24 | // |
| 25 | // as: return A ? true : B && C. |
| 26 | // |
| 27 | // There's code in SimplifyCFG that code be used to turn control flow in data |
| 28 | // flow using SelectInsts. Selects are slow on some architectures (P7/P8), so |
| 29 | // this probably isn't good in general, but for the special case of i1, the |
| 30 | // Selects could be further lowered to bit operations that are fast everywhere. |
| 31 | // |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
| 34 | #include "PPC.h" |
Guozhi Wei | f31c56d | 2017-06-08 18:27:24 +0000 | [diff] [blame] | 35 | #include "PPCTargetMachine.h" |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/DenseMap.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/STLExtras.h" |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/SmallPtrSet.h" |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/SmallVector.h" |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 41 | #include "llvm/IR/Argument.h" |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 42 | #include "llvm/IR/Constants.h" |
| 43 | #include "llvm/IR/Dominators.h" |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 44 | #include "llvm/IR/Function.h" |
| 45 | #include "llvm/IR/Instruction.h" |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 46 | #include "llvm/IR/Instructions.h" |
| 47 | #include "llvm/IR/IntrinsicInst.h" |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 48 | #include "llvm/IR/OperandTraits.h" |
| 49 | #include "llvm/IR/Type.h" |
| 50 | #include "llvm/IR/Use.h" |
| 51 | #include "llvm/IR/User.h" |
| 52 | #include "llvm/IR/Value.h" |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 53 | #include "llvm/Pass.h" |
Guozhi Wei | f31c56d | 2017-06-08 18:27:24 +0000 | [diff] [blame] | 54 | #include "llvm/CodeGen/TargetPassConfig.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 55 | #include "llvm/Support/Casting.h" |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 56 | #include <cassert> |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 57 | |
| 58 | using namespace llvm; |
| 59 | |
| 60 | namespace { |
| 61 | |
| 62 | #define DEBUG_TYPE "bool-ret-to-int" |
| 63 | |
| 64 | STATISTIC(NumBoolRetPromotion, |
| 65 | "Number of times a bool feeding a RetInst was promoted to an int"); |
| 66 | STATISTIC(NumBoolCallPromotion, |
| 67 | "Number of times a bool feeding a CallInst was promoted to an int"); |
| 68 | STATISTIC(NumBoolToIntPromotion, |
| 69 | "Total number of times a bool was promoted to an int"); |
| 70 | |
| 71 | class PPCBoolRetToInt : public FunctionPass { |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 72 | static SmallPtrSet<Value *, 8> findAllDefs(Value *V) { |
| 73 | SmallPtrSet<Value *, 8> Defs; |
| 74 | SmallVector<Value *, 8> WorkList; |
| 75 | WorkList.push_back(V); |
| 76 | Defs.insert(V); |
| 77 | while (!WorkList.empty()) { |
| 78 | Value *Curr = WorkList.back(); |
| 79 | WorkList.pop_back(); |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 80 | auto *CurrUser = dyn_cast<User>(Curr); |
Guozhi Wei | 9584d18 | 2016-08-03 21:43:51 +0000 | [diff] [blame] | 81 | // Operands of CallInst are skipped because they may not be Bool type, |
| 82 | // and their positions are defined by ABI. |
| 83 | if (CurrUser && !isa<CallInst>(Curr)) |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 84 | for (auto &Op : CurrUser->operands()) |
| 85 | if (Defs.insert(Op).second) |
| 86 | WorkList.push_back(Op); |
| 87 | } |
| 88 | return Defs; |
| 89 | } |
| 90 | |
Guozhi Wei | f31c56d | 2017-06-08 18:27:24 +0000 | [diff] [blame] | 91 | // Translate a i1 value to an equivalent i32/i64 value: |
| 92 | Value *translate(Value *V) { |
| 93 | Type *IntTy = ST->isPPC64() ? Type::getInt64Ty(V->getContext()) |
| 94 | : Type::getInt32Ty(V->getContext()); |
| 95 | |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 96 | if (auto *C = dyn_cast<Constant>(V)) |
Guozhi Wei | f31c56d | 2017-06-08 18:27:24 +0000 | [diff] [blame] | 97 | return ConstantExpr::getZExt(C, IntTy); |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 98 | if (auto *P = dyn_cast<PHINode>(V)) { |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 99 | // Temporarily set the operands to 0. We'll fix this later in |
| 100 | // runOnUse. |
Guozhi Wei | f31c56d | 2017-06-08 18:27:24 +0000 | [diff] [blame] | 101 | Value *Zero = Constant::getNullValue(IntTy); |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 102 | PHINode *Q = |
Guozhi Wei | f31c56d | 2017-06-08 18:27:24 +0000 | [diff] [blame] | 103 | PHINode::Create(IntTy, P->getNumIncomingValues(), P->getName(), P); |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 104 | for (unsigned i = 0; i < P->getNumOperands(); ++i) |
| 105 | Q->addIncoming(Zero, P->getIncomingBlock(i)); |
| 106 | return Q; |
| 107 | } |
| 108 | |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 109 | auto *A = dyn_cast<Argument>(V); |
| 110 | auto *I = dyn_cast<Instruction>(V); |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 111 | assert((A || I) && "Unknown value type"); |
| 112 | |
| 113 | auto InstPt = |
| 114 | A ? &*A->getParent()->getEntryBlock().begin() : I->getNextNode(); |
Guozhi Wei | f31c56d | 2017-06-08 18:27:24 +0000 | [diff] [blame] | 115 | return new ZExtInst(V, IntTy, "", InstPt); |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | typedef SmallPtrSet<const PHINode *, 8> PHINodeSet; |
| 119 | |
| 120 | // A PHINode is Promotable if: |
| 121 | // 1. Its type is i1 AND |
| 122 | // 2. All of its uses are ReturnInt, CallInst, PHINode, or DbgInfoIntrinsic |
| 123 | // AND |
| 124 | // 3. All of its operands are Constant or Argument or |
| 125 | // CallInst or PHINode AND |
| 126 | // 4. All of its PHINode uses are Promotable AND |
| 127 | // 5. All of its PHINode operands are Promotable |
| 128 | static PHINodeSet getPromotablePHINodes(const Function &F) { |
| 129 | PHINodeSet Promotable; |
| 130 | // Condition 1 |
| 131 | for (auto &BB : F) |
| 132 | for (auto &I : BB) |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 133 | if (const auto *P = dyn_cast<PHINode>(&I)) |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 134 | if (P->getType()->isIntegerTy(1)) |
| 135 | Promotable.insert(P); |
| 136 | |
| 137 | SmallVector<const PHINode *, 8> ToRemove; |
Benjamin Kramer | 451f54c | 2016-02-22 13:11:58 +0000 | [diff] [blame] | 138 | for (const PHINode *P : Promotable) { |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 139 | // Condition 2 and 3 |
| 140 | auto IsValidUser = [] (const Value *V) -> bool { |
| 141 | return isa<ReturnInst>(V) || isa<CallInst>(V) || isa<PHINode>(V) || |
| 142 | isa<DbgInfoIntrinsic>(V); |
| 143 | }; |
| 144 | auto IsValidOperand = [] (const Value *V) -> bool { |
| 145 | return isa<Constant>(V) || isa<Argument>(V) || isa<CallInst>(V) || |
| 146 | isa<PHINode>(V); |
| 147 | }; |
| 148 | const auto &Users = P->users(); |
| 149 | const auto &Operands = P->operands(); |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 150 | if (!llvm::all_of(Users, IsValidUser) || |
| 151 | !llvm::all_of(Operands, IsValidOperand)) |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 152 | ToRemove.push_back(P); |
| 153 | } |
| 154 | |
| 155 | // Iterate to convergence |
| 156 | auto IsPromotable = [&Promotable] (const Value *V) -> bool { |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 157 | const auto *Phi = dyn_cast<PHINode>(V); |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 158 | return !Phi || Promotable.count(Phi); |
| 159 | }; |
| 160 | while (!ToRemove.empty()) { |
| 161 | for (auto &User : ToRemove) |
| 162 | Promotable.erase(User); |
| 163 | ToRemove.clear(); |
| 164 | |
Benjamin Kramer | 451f54c | 2016-02-22 13:11:58 +0000 | [diff] [blame] | 165 | for (const PHINode *P : Promotable) { |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 166 | // Condition 4 and 5 |
| 167 | const auto &Users = P->users(); |
| 168 | const auto &Operands = P->operands(); |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 169 | if (!llvm::all_of(Users, IsPromotable) || |
| 170 | !llvm::all_of(Operands, IsPromotable)) |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 171 | ToRemove.push_back(P); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return Promotable; |
| 176 | } |
| 177 | |
| 178 | typedef DenseMap<Value *, Value *> B2IMap; |
| 179 | |
| 180 | public: |
| 181 | static char ID; |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 182 | |
Eric Christopher | 9fd267c | 2017-03-31 02:16:54 +0000 | [diff] [blame] | 183 | PPCBoolRetToInt() : FunctionPass(ID) { |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 184 | initializePPCBoolRetToIntPass(*PassRegistry::getPassRegistry()); |
| 185 | } |
| 186 | |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 187 | bool runOnFunction(Function &F) override { |
Andrew Kaylor | 289bd5f | 2016-04-27 19:39:32 +0000 | [diff] [blame] | 188 | if (skipFunction(F)) |
| 189 | return false; |
| 190 | |
Guozhi Wei | f31c56d | 2017-06-08 18:27:24 +0000 | [diff] [blame] | 191 | auto *TPC = getAnalysisIfAvailable<TargetPassConfig>(); |
| 192 | if (!TPC) |
| 193 | return false; |
| 194 | |
| 195 | auto &TM = TPC->getTM<PPCTargetMachine>(); |
| 196 | ST = TM.getSubtargetImpl(F); |
| 197 | |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 198 | PHINodeSet PromotablePHINodes = getPromotablePHINodes(F); |
| 199 | B2IMap Bool2IntMap; |
| 200 | bool Changed = false; |
| 201 | for (auto &BB : F) { |
| 202 | for (auto &I : BB) { |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 203 | if (auto *R = dyn_cast<ReturnInst>(&I)) |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 204 | if (F.getReturnType()->isIntegerTy(1)) |
| 205 | Changed |= |
| 206 | runOnUse(R->getOperandUse(0), PromotablePHINodes, Bool2IntMap); |
| 207 | |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 208 | if (auto *CI = dyn_cast<CallInst>(&I)) |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 209 | for (auto &U : CI->operands()) |
| 210 | if (U->getType()->isIntegerTy(1)) |
| 211 | Changed |= runOnUse(U, PromotablePHINodes, Bool2IntMap); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | return Changed; |
| 216 | } |
| 217 | |
Guozhi Wei | f31c56d | 2017-06-08 18:27:24 +0000 | [diff] [blame] | 218 | bool runOnUse(Use &U, const PHINodeSet &PromotablePHINodes, |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 219 | B2IMap &BoolToIntMap) { |
| 220 | auto Defs = findAllDefs(U); |
| 221 | |
| 222 | // If the values are all Constants or Arguments, don't bother |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 223 | if (llvm::none_of(Defs, isa<Instruction, Value *>)) |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 224 | return false; |
| 225 | |
Guozhi Wei | 9584d18 | 2016-08-03 21:43:51 +0000 | [diff] [blame] | 226 | // Presently, we only know how to handle PHINode, Constant, Arguments and |
| 227 | // CallInst. Potentially, bitwise operations (AND, OR, XOR, NOT) and sign |
| 228 | // extension could also be handled in the future. |
Benjamin Kramer | 451f54c | 2016-02-22 13:11:58 +0000 | [diff] [blame] | 229 | for (Value *V : Defs) |
Guozhi Wei | 9584d18 | 2016-08-03 21:43:51 +0000 | [diff] [blame] | 230 | if (!isa<PHINode>(V) && !isa<Constant>(V) && |
| 231 | !isa<Argument>(V) && !isa<CallInst>(V)) |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 232 | return false; |
| 233 | |
Benjamin Kramer | 451f54c | 2016-02-22 13:11:58 +0000 | [diff] [blame] | 234 | for (Value *V : Defs) |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 235 | if (const auto *P = dyn_cast<PHINode>(V)) |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 236 | if (!PromotablePHINodes.count(P)) |
| 237 | return false; |
| 238 | |
| 239 | if (isa<ReturnInst>(U.getUser())) |
| 240 | ++NumBoolRetPromotion; |
| 241 | if (isa<CallInst>(U.getUser())) |
| 242 | ++NumBoolCallPromotion; |
| 243 | ++NumBoolToIntPromotion; |
| 244 | |
Benjamin Kramer | 451f54c | 2016-02-22 13:11:58 +0000 | [diff] [blame] | 245 | for (Value *V : Defs) |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 246 | if (!BoolToIntMap.count(V)) |
| 247 | BoolToIntMap[V] = translate(V); |
| 248 | |
Guozhi Wei | 9584d18 | 2016-08-03 21:43:51 +0000 | [diff] [blame] | 249 | // Replace the operands of the translated instructions. They were set to |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 250 | // zero in the translate function. |
| 251 | for (auto &Pair : BoolToIntMap) { |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 252 | auto *First = dyn_cast<User>(Pair.first); |
| 253 | auto *Second = dyn_cast<User>(Pair.second); |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 254 | assert((!First || Second) && "translated from user to non-user!?"); |
Guozhi Wei | 9584d18 | 2016-08-03 21:43:51 +0000 | [diff] [blame] | 255 | // Operands of CallInst are skipped because they may not be Bool type, |
| 256 | // and their positions are defined by ABI. |
| 257 | if (First && !isa<CallInst>(First)) |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 258 | for (unsigned i = 0; i < First->getNumOperands(); ++i) |
| 259 | Second->setOperand(i, BoolToIntMap[First->getOperand(i)]); |
| 260 | } |
| 261 | |
| 262 | Value *IntRetVal = BoolToIntMap[U]; |
| 263 | Type *Int1Ty = Type::getInt1Ty(U->getContext()); |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 264 | auto *I = cast<Instruction>(U.getUser()); |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 265 | Value *BackToBool = new TruncInst(IntRetVal, Int1Ty, "backToBool", I); |
| 266 | U.set(BackToBool); |
| 267 | |
| 268 | return true; |
| 269 | } |
| 270 | |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 271 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 272 | AU.addPreserved<DominatorTreeWrapperPass>(); |
| 273 | FunctionPass::getAnalysisUsage(AU); |
| 274 | } |
Guozhi Wei | f31c56d | 2017-06-08 18:27:24 +0000 | [diff] [blame] | 275 | |
| 276 | private: |
| 277 | const PPCSubtarget *ST; |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 278 | }; |
Eugene Zelenko | 6a9226d | 2016-12-12 22:23:53 +0000 | [diff] [blame] | 279 | |
| 280 | } // end anonymous namespace |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 281 | |
| 282 | char PPCBoolRetToInt::ID = 0; |
Eric Christopher | 9fd267c | 2017-03-31 02:16:54 +0000 | [diff] [blame] | 283 | INITIALIZE_PASS(PPCBoolRetToInt, "bool-ret-to-int", |
Guozhi Wei | f31c56d | 2017-06-08 18:27:24 +0000 | [diff] [blame] | 284 | "Convert i1 constants to i32/i64 if they are returned", |
Eric Christopher | 9fd267c | 2017-03-31 02:16:54 +0000 | [diff] [blame] | 285 | false, false) |
Kit Barton | a1c712f | 2015-12-07 20:50:29 +0000 | [diff] [blame] | 286 | |
Eric Christopher | 9fd267c | 2017-03-31 02:16:54 +0000 | [diff] [blame] | 287 | FunctionPass *llvm::createPPCBoolRetToIntPass() { return new PPCBoolRetToInt(); } |