- 
                Notifications
    You must be signed in to change notification settings 
- Fork 15k
[InstCombine] Fold reconstruction across select #145102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            macsencasaus
  wants to merge
  6
  commits into
  llvm:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
macsencasaus:144020
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from 2 commits
      Commits
    
    
            Show all changes
          
          
            6 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      683da5c
              
                pre-commit test
              
              
                macsencasaus 6f0a946
              
                [InstCombine] Fold reconstruction across select
              
              
                macsencasaus 9542631
              
                address review comments
              
              
                macsencasaus f5bcc06
              
                match correct mask only
              
              
                macsencasaus 039432c
              
                match constant V2 value only
              
              
                 1ec984a
              
                match V1 as constant
              
              
                 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -1349,6 +1349,37 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I, | |
| return nullptr; | ||
| }; | ||
|  | ||
| // Special case for reconstructing across a select: | ||
| // (Cond ? V1 : (X & Mask)) op | ||
| // zext (Cond ? V2 : trunc X) | ||
| // -> (Cond ? (V1 op zext V2) : ((X & Mask) op zext trunc X)) | ||
| auto foldReconstruction = [&](Value *V1, Value *Masked, | ||
| Value *ZExtSel) -> Value * { | ||
| Value *X; | ||
| if (!match(Masked, m_OneUse(m_And(m_Value(X), m_Constant())))) | ||
| return nullptr; | ||
|  | ||
| Value *V2, *Trunc; | ||
| if (!match(ZExtSel, m_ZExt(m_OneUse(m_Select(m_Specific(Cond), m_Value(V2), | ||
| m_Value(Trunc)))))) | ||
| return nullptr; | ||
|  | ||
| if (!match(Trunc, m_Trunc(m_Specific(X)))) | ||
| return nullptr; | ||
|  | ||
| Value *ZExtTrue = Builder.CreateZExt(V2, V1->getType()); | ||
|          | ||
| Value *True; | ||
| if (!(True = simplifyBinOp(Opcode, V1, ZExtTrue, FMF, Q))) | ||
| True = Builder.CreateOr(V1, ZExtTrue); | ||
|         
                  dtcxzyw marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| Value *ZExtFalse = Builder.CreateZExt(Trunc, V1->getType()); | ||
| Value *False; | ||
| if (!(False = simplifyBinOp(Opcode, Masked, ZExtFalse, FMF, Q))) | ||
| False = Builder.CreateOr(Masked, ZExtFalse); | ||
|  | ||
| return Builder.CreateSelect(Cond, True, False, I.getName()); | ||
|         
                  dtcxzyw marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| }; | ||
|  | ||
| if (LHSIsSelect && RHSIsSelect && A == D) { | ||
| // (A ? B : C) op (A ? E : F) -> A ? (B op E) : (C op F) | ||
| Cond = A; | ||
|  | @@ -1368,13 +1399,17 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I, | |
| False = simplifyBinOp(Opcode, C, RHS, FMF, Q); | ||
| if (Value *NewSel = foldAddNegate(B, C, RHS)) | ||
| return NewSel; | ||
| if (Value *NewSel = foldReconstruction(B, C, RHS)) | ||
| return NewSel; | ||
| } else if (RHSIsSelect && RHS->hasOneUse()) { | ||
| // X op (D ? E : F) -> D ? (X op E) : (X op F) | ||
| Cond = D; | ||
| True = simplifyBinOp(Opcode, LHS, E, FMF, Q); | ||
| False = simplifyBinOp(Opcode, LHS, F, FMF, Q); | ||
| if (Value *NewSel = foldAddNegate(E, F, LHS)) | ||
| return NewSel; | ||
| if (Value *NewSel = foldReconstruction(E, F, LHS)) | ||
| return NewSel; | ||
| } | ||
|  | ||
| if (!True || !False) | ||
|  | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 | ||
| ; RUN: opt < %s -passes=instcombine -S | FileCheck %s | ||
|  | ||
| define i40 @select_reconstruction_i40(i40 %arg0) { | ||
| ; CHECK-LABEL: define i40 @select_reconstruction_i40( | ||
| ; CHECK-SAME: i40 [[ARG0:%.*]]) { | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = trunc i40 [[ARG0]] to i8 | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 2 | ||
| ; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP2]], i40 0, i40 [[ARG0]] | ||
| ; CHECK-NEXT: ret i40 [[TMP3]] | ||
| ; | ||
| %1 = trunc i40 %arg0 to i8 | ||
|         
                  dtcxzyw marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| %2 = icmp eq i8 %1, 2 | ||
| %3 = and i40 %arg0, -256 | ||
| %4 = select i1 %2, i8 0, i8 %1 | ||
| %5 = select i1 %2, i40 0, i40 %3 | ||
| %6 = zext i8 %4 to i40 | ||
| %7 = or disjoint i40 %5, %6 | ||
| ret i40 %7 | ||
| } | ||
|  | ||
| define i40 @select_reconstruction_any_cmp_val(i40 %arg0, i8 %arg1) { | ||
| ; CHECK-LABEL: define i40 @select_reconstruction_any_cmp_val( | ||
| ; CHECK-SAME: i40 [[ARG0:%.*]], i8 [[ARG1:%.*]]) { | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = trunc i40 [[ARG0]] to i8 | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[ARG1]], [[TMP1]] | ||
| ; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP2]], i40 0, i40 [[ARG0]] | ||
| ; CHECK-NEXT: ret i40 [[TMP3]] | ||
| ; | ||
| %1 = trunc i40 %arg0 to i8 | ||
| %2 = icmp eq i8 %1, %arg1 | ||
| %3 = and i40 %arg0, -256 | ||
| %4 = select i1 %2, i8 0, i8 %1 | ||
| %5 = select i1 %2, i40 0, i40 %3 | ||
| %6 = zext i8 %4 to i40 | ||
| %7 = or disjoint i40 %5, %6 | ||
| ret i40 %7 | ||
| } | ||
|  | ||
| define i40 @select_reconstruction_257_mask(i40 %arg0) { | ||
| ; CHECK-LABEL: define i40 @select_reconstruction_257_mask( | ||
| ; CHECK-SAME: i40 [[ARG0:%.*]]) { | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = trunc i40 [[ARG0]] to i8 | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 2 | ||
| ; CHECK-NEXT: [[TMP3:%.*]] = and i40 [[ARG0]], -257 | ||
| ; CHECK-NEXT: [[TMP4:%.*]] = select i1 [[TMP2]], i40 0, i40 [[TMP3]] | ||
| ; CHECK-NEXT: ret i40 [[TMP4]] | ||
| ; | ||
| %1 = trunc i40 %arg0 to i8 | ||
| %2 = icmp eq i8 %1, 2 | ||
| %3 = and i40 %arg0, -257 | ||
| %4 = select i1 %2, i8 0, i8 %1 | ||
| %5 = select i1 %2, i40 0, i40 %3 | ||
| %6 = zext i8 %4 to i40 | ||
| %7 = or disjoint i40 %5, %6 | ||
| ret i40 %7 | ||
| } | ||
|  | ||
| define i40 @select_reconstruction_i16_mask(i40 %arg0) { | ||
| ; CHECK-LABEL: define i40 @select_reconstruction_i16_mask( | ||
| ; CHECK-SAME: i40 [[ARG0:%.*]]) { | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = trunc i40 [[ARG0]] to i16 | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i16 [[TMP1]], 2 | ||
| ; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP2]], i40 0, i40 [[ARG0]] | ||
| ; CHECK-NEXT: ret i40 [[TMP3]] | ||
| ; | ||
| %1 = trunc i40 %arg0 to i16 | ||
| %2 = icmp eq i16 %1, 2 | ||
| %3 = and i40 %arg0, -65356 | ||
| %4 = select i1 %2, i16 0, i16 %1 | ||
| %5 = select i1 %2, i40 0, i40 %3 | ||
| %6 = zext i16 %4 to i40 | ||
| %7 = or disjoint i40 %5, %6 | ||
| ret i40 %7 | ||
| } | ||
|  | ||
| define <2 x i32> @select_reconstruction_vec_any_cmp_val(<2 x i32> %arg0, <2 x i8> %arg1) { | ||
| ; CHECK-LABEL: define <2 x i32> @select_reconstruction_vec_any_cmp_val( | ||
| ; CHECK-SAME: <2 x i32> [[ARG0:%.*]], <2 x i8> [[ARG1:%.*]]) { | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = trunc <2 x i32> [[ARG0]] to <2 x i8> | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = icmp eq <2 x i8> [[ARG1]], [[TMP1]] | ||
| ; CHECK-NEXT: [[TMP7:%.*]] = select <2 x i1> [[TMP2]], <2 x i32> zeroinitializer, <2 x i32> [[ARG0]] | ||
| ; CHECK-NEXT: ret <2 x i32> [[TMP7]] | ||
| ; | ||
| %1 = trunc <2 x i32> %arg0 to <2 x i8> | ||
| %2 = icmp eq <2 x i8> %1, %arg1 | ||
| %3 = and <2 x i32> %arg0, <i32 -256, i32 -256> | ||
| %4 = select <2 x i1> %2, <2 x i8> <i8 0, i8 0>, <2 x i8> %1 | ||
| %5 = select <2 x i1> %2, <2 x i32> <i32 0, i32 0>, <2 x i32> %3 | ||
| %6 = zext <2 x i8> %4 to <2 x i32> | ||
| %7 = or <2 x i32> %5, %6 | ||
| ret <2 x i32> %7 | ||
| } | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing check for the mask. It should be
APInt::getBitsSetFrom(X->getType()->getScalarSizeInBits(), Trunc->getType()->getScalarSizeInBits()).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought of this, but it would fail to simplify a case like
llvm-project/llvm/test/Transforms/InstCombine/select-reconstruction.ll
Lines 40 to 57 in 6f0a946
I can do it anyway if you believe this case is not relevant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it should work for all constants. It even holds if we replace the
andwith any other instructions. I'd like to focus on the original motivating issue if you don't have a better idea to generalize the pattern. With this constraint, we can simplify((X & Mask) op zext trunc X)intoXdirectly, instead of creating a temporary instruction and relying on later optimizations.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To generalize, this optimization already occurs when the condition is arbitrary:
https://godbolt.org/z/8EPEP81TP
It just doesn't work when the condition is an
icmpbecause of this check:llvm-project/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
Lines 178 to 186 in cfcb788
I didn't know how to implement the optimization from here, but this may be an area where a more general optimization might occur.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is exactly what I said in #145102 (comment).