blob: 55e105dad0e5b4736a1c7d894708d285c78ca66d [file] [log] [blame]
Eugene Zelenko6a9226d2016-12-12 22:23:53 +00001//===- PPCBoolRetToInt.cpp ------------------------------------------------===//
Kit Bartona1c712f2015-12-07 20:50:29 +00002//
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//
Guozhi Weif31c56d2017-06-08 18:27:24 +000010// This file implements converting i1 values to i32/i64 if they could be more
Kit Bartona1c712f2015-12-07 20:50:29 +000011// profitably allocated as GPRs rather than CRs. This pass will become totally
12// unnecessary if Register Bank Allocation and Global Instruction Selection ever
13// go upstream.
14//
Guozhi Weif31c56d2017-06-08 18:27:24 +000015// Presently, the pass converts i1 Constants, and Arguments to i32/i64 if the
Kit Bartona1c712f2015-12-07 20:50:29 +000016// transitive closure of their uses includes only PHINodes, CallInsts, and
17// ReturnInsts. The rational is that arguments are generally passed and returned
Guozhi Weif31c56d2017-06-08 18:27:24 +000018// in GPRs rather than CRs, so casting them to i32/i64 at the LLVM IR level will
Kit Bartona1c712f2015-12-07 20:50:29 +000019// actually save casts at the Machine Instruction level.
20//
21// It might be useful to expand this pass to add bit-wise operations to the list
22// of safe transitive closure types. Also, we miss some opportunities when LLVM
23// represents logical AND and OR operations with control flow rather than data
24// flow. For example by lowering the expression: return (A && B && C)
25//
26// as: return A ? true : B && C.
27//
28// There's code in SimplifyCFG that code be used to turn control flow in data
29// flow using SelectInsts. Selects are slow on some architectures (P7/P8), so
30// this probably isn't good in general, but for the special case of i1, the
31// Selects could be further lowered to bit operations that are fast everywhere.
32//
33//===----------------------------------------------------------------------===//
34
35#include "PPC.h"
Guozhi Weif31c56d2017-06-08 18:27:24 +000036#include "PPCTargetMachine.h"
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000037#include "llvm/ADT/DenseMap.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000038#include "llvm/ADT/STLExtras.h"
Kit Bartona1c712f2015-12-07 20:50:29 +000039#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000040#include "llvm/ADT/SmallVector.h"
Kit Bartona1c712f2015-12-07 20:50:29 +000041#include "llvm/ADT/Statistic.h"
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000042#include "llvm/IR/Argument.h"
Kit Bartona1c712f2015-12-07 20:50:29 +000043#include "llvm/IR/Constants.h"
44#include "llvm/IR/Dominators.h"
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000045#include "llvm/IR/Function.h"
46#include "llvm/IR/Instruction.h"
Kit Bartona1c712f2015-12-07 20:50:29 +000047#include "llvm/IR/Instructions.h"
48#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000049#include "llvm/IR/OperandTraits.h"
50#include "llvm/IR/Type.h"
51#include "llvm/IR/Use.h"
52#include "llvm/IR/User.h"
53#include "llvm/IR/Value.h"
Kit Bartona1c712f2015-12-07 20:50:29 +000054#include "llvm/Pass.h"
Guozhi Weif31c56d2017-06-08 18:27:24 +000055#include "llvm/CodeGen/TargetPassConfig.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000056#include "llvm/Support/Casting.h"
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000057#include <cassert>
Kit Bartona1c712f2015-12-07 20:50:29 +000058
59using namespace llvm;
60
61namespace {
62
63#define DEBUG_TYPE "bool-ret-to-int"
64
65STATISTIC(NumBoolRetPromotion,
66 "Number of times a bool feeding a RetInst was promoted to an int");
67STATISTIC(NumBoolCallPromotion,
68 "Number of times a bool feeding a CallInst was promoted to an int");
69STATISTIC(NumBoolToIntPromotion,
70 "Total number of times a bool was promoted to an int");
71
72class PPCBoolRetToInt : public FunctionPass {
Kit Bartona1c712f2015-12-07 20:50:29 +000073 static SmallPtrSet<Value *, 8> findAllDefs(Value *V) {
74 SmallPtrSet<Value *, 8> Defs;
75 SmallVector<Value *, 8> WorkList;
76 WorkList.push_back(V);
77 Defs.insert(V);
78 while (!WorkList.empty()) {
79 Value *Curr = WorkList.back();
80 WorkList.pop_back();
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000081 auto *CurrUser = dyn_cast<User>(Curr);
Guozhi Wei9584d182016-08-03 21:43:51 +000082 // Operands of CallInst are skipped because they may not be Bool type,
83 // and their positions are defined by ABI.
84 if (CurrUser && !isa<CallInst>(Curr))
Kit Bartona1c712f2015-12-07 20:50:29 +000085 for (auto &Op : CurrUser->operands())
86 if (Defs.insert(Op).second)
87 WorkList.push_back(Op);
88 }
89 return Defs;
90 }
91
Guozhi Weif31c56d2017-06-08 18:27:24 +000092 // Translate a i1 value to an equivalent i32/i64 value:
93 Value *translate(Value *V) {
94 Type *IntTy = ST->isPPC64() ? Type::getInt64Ty(V->getContext())
95 : Type::getInt32Ty(V->getContext());
96
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000097 if (auto *C = dyn_cast<Constant>(V))
Guozhi Weif31c56d2017-06-08 18:27:24 +000098 return ConstantExpr::getZExt(C, IntTy);
Eugene Zelenko6a9226d2016-12-12 22:23:53 +000099 if (auto *P = dyn_cast<PHINode>(V)) {
Kit Bartona1c712f2015-12-07 20:50:29 +0000100 // Temporarily set the operands to 0. We'll fix this later in
101 // runOnUse.
Guozhi Weif31c56d2017-06-08 18:27:24 +0000102 Value *Zero = Constant::getNullValue(IntTy);
Kit Bartona1c712f2015-12-07 20:50:29 +0000103 PHINode *Q =
Guozhi Weif31c56d2017-06-08 18:27:24 +0000104 PHINode::Create(IntTy, P->getNumIncomingValues(), P->getName(), P);
Kit Bartona1c712f2015-12-07 20:50:29 +0000105 for (unsigned i = 0; i < P->getNumOperands(); ++i)
106 Q->addIncoming(Zero, P->getIncomingBlock(i));
107 return Q;
108 }
109
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000110 auto *A = dyn_cast<Argument>(V);
111 auto *I = dyn_cast<Instruction>(V);
Kit Bartona1c712f2015-12-07 20:50:29 +0000112 assert((A || I) && "Unknown value type");
113
114 auto InstPt =
115 A ? &*A->getParent()->getEntryBlock().begin() : I->getNextNode();
Guozhi Weif31c56d2017-06-08 18:27:24 +0000116 return new ZExtInst(V, IntTy, "", InstPt);
Kit Bartona1c712f2015-12-07 20:50:29 +0000117 }
118
119 typedef SmallPtrSet<const PHINode *, 8> PHINodeSet;
120
121 // A PHINode is Promotable if:
122 // 1. Its type is i1 AND
123 // 2. All of its uses are ReturnInt, CallInst, PHINode, or DbgInfoIntrinsic
124 // AND
125 // 3. All of its operands are Constant or Argument or
126 // CallInst or PHINode AND
127 // 4. All of its PHINode uses are Promotable AND
128 // 5. All of its PHINode operands are Promotable
129 static PHINodeSet getPromotablePHINodes(const Function &F) {
130 PHINodeSet Promotable;
131 // Condition 1
132 for (auto &BB : F)
133 for (auto &I : BB)
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000134 if (const auto *P = dyn_cast<PHINode>(&I))
Kit Bartona1c712f2015-12-07 20:50:29 +0000135 if (P->getType()->isIntegerTy(1))
136 Promotable.insert(P);
137
138 SmallVector<const PHINode *, 8> ToRemove;
Benjamin Kramer451f54c2016-02-22 13:11:58 +0000139 for (const PHINode *P : Promotable) {
Kit Bartona1c712f2015-12-07 20:50:29 +0000140 // Condition 2 and 3
141 auto IsValidUser = [] (const Value *V) -> bool {
142 return isa<ReturnInst>(V) || isa<CallInst>(V) || isa<PHINode>(V) ||
143 isa<DbgInfoIntrinsic>(V);
144 };
145 auto IsValidOperand = [] (const Value *V) -> bool {
146 return isa<Constant>(V) || isa<Argument>(V) || isa<CallInst>(V) ||
147 isa<PHINode>(V);
148 };
149 const auto &Users = P->users();
150 const auto &Operands = P->operands();
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000151 if (!llvm::all_of(Users, IsValidUser) ||
152 !llvm::all_of(Operands, IsValidOperand))
Kit Bartona1c712f2015-12-07 20:50:29 +0000153 ToRemove.push_back(P);
154 }
155
156 // Iterate to convergence
157 auto IsPromotable = [&Promotable] (const Value *V) -> bool {
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000158 const auto *Phi = dyn_cast<PHINode>(V);
Kit Bartona1c712f2015-12-07 20:50:29 +0000159 return !Phi || Promotable.count(Phi);
160 };
161 while (!ToRemove.empty()) {
162 for (auto &User : ToRemove)
163 Promotable.erase(User);
164 ToRemove.clear();
165
Benjamin Kramer451f54c2016-02-22 13:11:58 +0000166 for (const PHINode *P : Promotable) {
Kit Bartona1c712f2015-12-07 20:50:29 +0000167 // Condition 4 and 5
168 const auto &Users = P->users();
169 const auto &Operands = P->operands();
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000170 if (!llvm::all_of(Users, IsPromotable) ||
171 !llvm::all_of(Operands, IsPromotable))
Kit Bartona1c712f2015-12-07 20:50:29 +0000172 ToRemove.push_back(P);
173 }
174 }
175
176 return Promotable;
177 }
178
179 typedef DenseMap<Value *, Value *> B2IMap;
180
181 public:
182 static char ID;
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000183
Eric Christopher9fd267c2017-03-31 02:16:54 +0000184 PPCBoolRetToInt() : FunctionPass(ID) {
Kit Bartona1c712f2015-12-07 20:50:29 +0000185 initializePPCBoolRetToIntPass(*PassRegistry::getPassRegistry());
186 }
187
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000188 bool runOnFunction(Function &F) override {
Andrew Kaylor289bd5f2016-04-27 19:39:32 +0000189 if (skipFunction(F))
190 return false;
191
Guozhi Weif31c56d2017-06-08 18:27:24 +0000192 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
193 if (!TPC)
194 return false;
195
196 auto &TM = TPC->getTM<PPCTargetMachine>();
197 ST = TM.getSubtargetImpl(F);
198
Kit Bartona1c712f2015-12-07 20:50:29 +0000199 PHINodeSet PromotablePHINodes = getPromotablePHINodes(F);
200 B2IMap Bool2IntMap;
201 bool Changed = false;
202 for (auto &BB : F) {
203 for (auto &I : BB) {
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000204 if (auto *R = dyn_cast<ReturnInst>(&I))
Kit Bartona1c712f2015-12-07 20:50:29 +0000205 if (F.getReturnType()->isIntegerTy(1))
206 Changed |=
207 runOnUse(R->getOperandUse(0), PromotablePHINodes, Bool2IntMap);
208
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000209 if (auto *CI = dyn_cast<CallInst>(&I))
Kit Bartona1c712f2015-12-07 20:50:29 +0000210 for (auto &U : CI->operands())
211 if (U->getType()->isIntegerTy(1))
212 Changed |= runOnUse(U, PromotablePHINodes, Bool2IntMap);
213 }
214 }
215
216 return Changed;
217 }
218
Guozhi Weif31c56d2017-06-08 18:27:24 +0000219 bool runOnUse(Use &U, const PHINodeSet &PromotablePHINodes,
Kit Bartona1c712f2015-12-07 20:50:29 +0000220 B2IMap &BoolToIntMap) {
221 auto Defs = findAllDefs(U);
222
223 // If the values are all Constants or Arguments, don't bother
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000224 if (llvm::none_of(Defs, isa<Instruction, Value *>))
Kit Bartona1c712f2015-12-07 20:50:29 +0000225 return false;
226
Guozhi Wei9584d182016-08-03 21:43:51 +0000227 // Presently, we only know how to handle PHINode, Constant, Arguments and
228 // CallInst. Potentially, bitwise operations (AND, OR, XOR, NOT) and sign
229 // extension could also be handled in the future.
Benjamin Kramer451f54c2016-02-22 13:11:58 +0000230 for (Value *V : Defs)
Guozhi Wei9584d182016-08-03 21:43:51 +0000231 if (!isa<PHINode>(V) && !isa<Constant>(V) &&
232 !isa<Argument>(V) && !isa<CallInst>(V))
Kit Bartona1c712f2015-12-07 20:50:29 +0000233 return false;
234
Benjamin Kramer451f54c2016-02-22 13:11:58 +0000235 for (Value *V : Defs)
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000236 if (const auto *P = dyn_cast<PHINode>(V))
Kit Bartona1c712f2015-12-07 20:50:29 +0000237 if (!PromotablePHINodes.count(P))
238 return false;
239
240 if (isa<ReturnInst>(U.getUser()))
241 ++NumBoolRetPromotion;
242 if (isa<CallInst>(U.getUser()))
243 ++NumBoolCallPromotion;
244 ++NumBoolToIntPromotion;
245
Benjamin Kramer451f54c2016-02-22 13:11:58 +0000246 for (Value *V : Defs)
Kit Bartona1c712f2015-12-07 20:50:29 +0000247 if (!BoolToIntMap.count(V))
248 BoolToIntMap[V] = translate(V);
249
Guozhi Wei9584d182016-08-03 21:43:51 +0000250 // Replace the operands of the translated instructions. They were set to
Kit Bartona1c712f2015-12-07 20:50:29 +0000251 // zero in the translate function.
252 for (auto &Pair : BoolToIntMap) {
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000253 auto *First = dyn_cast<User>(Pair.first);
254 auto *Second = dyn_cast<User>(Pair.second);
Kit Bartona1c712f2015-12-07 20:50:29 +0000255 assert((!First || Second) && "translated from user to non-user!?");
Guozhi Wei9584d182016-08-03 21:43:51 +0000256 // Operands of CallInst are skipped because they may not be Bool type,
257 // and their positions are defined by ABI.
258 if (First && !isa<CallInst>(First))
Kit Bartona1c712f2015-12-07 20:50:29 +0000259 for (unsigned i = 0; i < First->getNumOperands(); ++i)
260 Second->setOperand(i, BoolToIntMap[First->getOperand(i)]);
261 }
262
263 Value *IntRetVal = BoolToIntMap[U];
264 Type *Int1Ty = Type::getInt1Ty(U->getContext());
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000265 auto *I = cast<Instruction>(U.getUser());
Kit Bartona1c712f2015-12-07 20:50:29 +0000266 Value *BackToBool = new TruncInst(IntRetVal, Int1Ty, "backToBool", I);
267 U.set(BackToBool);
268
269 return true;
270 }
271
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000272 void getAnalysisUsage(AnalysisUsage &AU) const override {
Kit Bartona1c712f2015-12-07 20:50:29 +0000273 AU.addPreserved<DominatorTreeWrapperPass>();
274 FunctionPass::getAnalysisUsage(AU);
275 }
Guozhi Weif31c56d2017-06-08 18:27:24 +0000276
277private:
278 const PPCSubtarget *ST;
Kit Bartona1c712f2015-12-07 20:50:29 +0000279};
Eugene Zelenko6a9226d2016-12-12 22:23:53 +0000280
281} // end anonymous namespace
Kit Bartona1c712f2015-12-07 20:50:29 +0000282
283char PPCBoolRetToInt::ID = 0;
Eric Christopher9fd267c2017-03-31 02:16:54 +0000284INITIALIZE_PASS(PPCBoolRetToInt, "bool-ret-to-int",
Guozhi Weif31c56d2017-06-08 18:27:24 +0000285 "Convert i1 constants to i32/i64 if they are returned",
Eric Christopher9fd267c2017-03-31 02:16:54 +0000286 false, false)
Kit Bartona1c712f2015-12-07 20:50:29 +0000287
Eric Christopher9fd267c2017-03-31 02:16:54 +0000288FunctionPass *llvm::createPPCBoolRetToIntPass() { return new PPCBoolRetToInt(); }